【问题标题】:PropertyNotFoundException: Could not find a setterPropertyNotFoundException:找不到二传手
【发布时间】:2016-10-17 20:36:06
【问题描述】:

假设我有这样的实体:

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: Could not locate setter method for property [Foo#empty]
我发现了一个类似的帖子 - HIbernate Mapping Exception: PropertyNotFoundException: Could not find a setter 并且有关于这种方法的 Trainsient 注释有所帮助。

【问题讨论】:

  • 您应该能够让 Hibernate 忽略您的方法,只需在 XML 中完全不包含它。

标签: java hibernate jpa


【解决方案1】:

通过指定&lt;transient name="isEmpty"/&gt;,您尝试向JPA 提供者表明您拥有一个名为isEmpty 的瞬态属性。您的属性实际上命名为 empty,而不是 isEmpty,错误消息也表明了这一点 (Foo#empty)。将对应的 XML 标签替换为&lt;transient name="empty"/&gt;

【讨论】:

    猜你喜欢
    • 2012-06-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-06
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 2015-01-29
    • 2016-08-16
    相关资源
    最近更新 更多