rvit34 rvit34 - 1 month ago 12
Java Question

PropertyNotFoundException: Could not find a setter

Suppose I have such entity:

public class Foo {

private long id;
private List<Bar> list = new ArrayList<>();

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}

public List<Bar> getList() {
return list;
}

public void setList(List<Bar> list) {
this.list = list;
}

/** helper method*/
public boolean isEmpty(){
return list.isEmpty();
}
}


And corresponding entity mapping:

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://xmlns.jcp.org/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence/orm"
version="2.1">

<entity class="Foo">
<table name="foo"/>
<attributes>
<id name="id"/>
<one-to-many name="list">
<!-- ... -->
</one-to-many>
<transient name="isEmpty"/>
</attributes>
</entity>

</entity-mappings>


And this exception I got:
org.hibernate.PropertyNotFoundException: Could not locate setter method for property [Foo#empty]


I found a similar post - HIbernate Mapping Exception: PropertyNotFoundException: Could not find a setter and there Trainsient annotation on such method helped.

Answer

By specifying <transient name="isEmpty"/> you try to signal to the JPA provider that you have a transient property named isEmpty. Your property is actually named empty, not isEmpty, and the error message indicates that too (Foo#empty). Replace the corresponding XML tag with <transient name="empty"/>.