假设我有这样的实体:
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();
}
}
以及对应的实体映射:
<?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>
我得到了这个异常:org.hibernate.PropertyNotFoundException:无法找到属性 [Foo#empty] 的 setter 方法
我发现了类似的帖子 - HIbernate Mapping Exception: PropertyNotFoundException: Could not find a setter这种方法的 Trainsient 注释很有帮助。
最佳答案
通过指定<transient name="isEmpty"/>
您尝试向 JPA 提供程序发出信号,表明您有一个名为 isEmpty
的 transient 属性。您的属性(property)实际上被命名为 empty
,不是isEmpty
,错误消息也表明了这一点 ( Foo#empty
)。将相应的 XML 标记替换为 <transient name="empty"/>
.
关于java - 属性未找到异常 : Could not find a setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40095224/