org.hibernate.PropertyNotFoundException: 在 class model.Contact 中找不到 ID 的 getter
org.hibernate.PropertyNotFoundException: Could not find a getter for id in class model.Contact
我正在尝试学习 Hibernate 并且正在学习本教程:http://www.visualcplusdotnet.com/javaopensource/javahibernateswingdesktopapp5.html 除了我使用的是自己的示例数据库。我已经为我的 contact
table 逆向设计了 POJO,看起来像这样:
Netbeans 自动生成了一个 .java 文件,如下所示 (Contact.java
):
package model;
// Generated Mar 6, 2015 12:04:10 AM by Hibernate Tools 4.3.1
/**
* Contact generated by hbm2java
*/
public class Contact implements java.io.Serializable {
private Integer id;
private String firstname;
private String lastname;
private String email;
private String phone;
private boolean active;
public Contact() {
}
public Contact(String firstname, String lastname, boolean active) {
this.firstname = firstname;
this.lastname = lastname;
this.active = active;
}
public Contact(String firstname, String lastname, String email, String phone, boolean active) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.phone = phone;
this.active = active;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public boolean isActive() {
return this.active;
}
public void setActive(boolean active) {
this.active = active;
}
}
和一个看起来像这样的 .xml 文件 (Contact.hbm.xml)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 6, 2015 12:04:12 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="model.Contact" table="contact" catalog="crmtool" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="firstname" type="string">
<column name="firstname" length="100" not-null="true" />
</property>
<property name="lastname" type="string">
<column name="lastname" length="100" not-null="true" />
</property>
<property name="email" type="string">
<column name="email" length="100" />
</property>
<property name="phone" type="string">
<column name="phone" length="10" />
</property>
<property name="active" type="boolean">
<column name="active" not-null="true" />
</property>
</class>
</hibernate-mapping>
清理和构建后,当我 运行 这个 HQL 查询时:
from Contact
我收到这个错误:
org.hibernate.PropertyNotFoundException: Could not find a getter for id in class model.Contact
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:310)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:304)
at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:497)
at org.hibernate.tuple.PropertyFactory.buildIdentifierAttribute(PropertyFactory.java:87)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:163)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:520)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:148)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:400)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
阅读这些问题的答案后:
- Hibernate - PropertyNotFoundException: Could not find a getter for
- PropertyNotFoundException: Could not find a getter for lastMoveDate in class com.hib.objects.GameBoard
- org.hibernate.PropertyNotFoundException: could not find getter for customerId in class Address
我认为 id
被 getter 大写的方式一定有问题,所以我尝试了:
public Integer getid() {
return this.id;
}
public int getId() {
return this.id;
}
public int getid() {
return this.id;
}
各种各样的事情,但仍然没有任何效果。
有谁知道我为什么会收到此错误消息?
这是:
您的配置看起来正确。可以做一个简单的测试,只需将您的 id 属性重命名为 contactId 并创建 getters 和 setter,相应地修改 Contact.hbm.xml 并查看 hibernate 是否抱怨无法为新的 getter 方法属性。感觉有一些项目清理问题
此致,
不要试图解决。将 id 列的名称更改为 "cid" 或 "contact_id" 或您想要的任何名称。
我正在尝试学习 Hibernate 并且正在学习本教程:http://www.visualcplusdotnet.com/javaopensource/javahibernateswingdesktopapp5.html 除了我使用的是自己的示例数据库。我已经为我的 contact
table 逆向设计了 POJO,看起来像这样:
Netbeans 自动生成了一个 .java 文件,如下所示 (Contact.java
):
package model;
// Generated Mar 6, 2015 12:04:10 AM by Hibernate Tools 4.3.1
/**
* Contact generated by hbm2java
*/
public class Contact implements java.io.Serializable {
private Integer id;
private String firstname;
private String lastname;
private String email;
private String phone;
private boolean active;
public Contact() {
}
public Contact(String firstname, String lastname, boolean active) {
this.firstname = firstname;
this.lastname = lastname;
this.active = active;
}
public Contact(String firstname, String lastname, String email, String phone, boolean active) {
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.phone = phone;
this.active = active;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getFirstname() {
return this.firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return this.lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getEmail() {
return this.email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return this.phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public boolean isActive() {
return this.active;
}
public void setActive(boolean active) {
this.active = active;
}
}
和一个看起来像这样的 .xml 文件 (Contact.hbm.xml)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<!-- Generated Mar 6, 2015 12:04:12 AM by Hibernate Tools 4.3.1 -->
<hibernate-mapping>
<class name="model.Contact" table="contact" catalog="crmtool" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="firstname" type="string">
<column name="firstname" length="100" not-null="true" />
</property>
<property name="lastname" type="string">
<column name="lastname" length="100" not-null="true" />
</property>
<property name="email" type="string">
<column name="email" length="100" />
</property>
<property name="phone" type="string">
<column name="phone" length="10" />
</property>
<property name="active" type="boolean">
<column name="active" not-null="true" />
</property>
</class>
</hibernate-mapping>
清理和构建后,当我 运行 这个 HQL 查询时:
from Contact
我收到这个错误:
org.hibernate.PropertyNotFoundException: Could not find a getter for id in class model.Contact
at org.hibernate.property.BasicPropertyAccessor.createGetter(BasicPropertyAccessor.java:310)
at org.hibernate.property.BasicPropertyAccessor.getGetter(BasicPropertyAccessor.java:304)
at org.hibernate.tuple.PropertyFactory.getGetter(PropertyFactory.java:497)
at org.hibernate.tuple.PropertyFactory.buildIdentifierAttribute(PropertyFactory.java:87)
at org.hibernate.tuple.entity.EntityMetamodel.<init>(EntityMetamodel.java:163)
at org.hibernate.persister.entity.AbstractEntityPersister.<init>(AbstractEntityPersister.java:520)
at org.hibernate.persister.entity.SingleTableEntityPersister.<init>(SingleTableEntityPersister.java:148)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
at org.hibernate.persister.internal.PersisterFactoryImpl.create(PersisterFactoryImpl.java:163)
at org.hibernate.persister.internal.PersisterFactoryImpl.createEntityPersister(PersisterFactoryImpl.java:135)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:400)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1857)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
阅读这些问题的答案后:
- Hibernate - PropertyNotFoundException: Could not find a getter for
- PropertyNotFoundException: Could not find a getter for lastMoveDate in class com.hib.objects.GameBoard
- org.hibernate.PropertyNotFoundException: could not find getter for customerId in class Address
我认为 id
被 getter 大写的方式一定有问题,所以我尝试了:
public Integer getid() {
return this.id;
}
public int getId() {
return this.id;
}
public int getid() {
return this.id;
}
各种各样的事情,但仍然没有任何效果。
有谁知道我为什么会收到此错误消息?
这是: 您的配置看起来正确。可以做一个简单的测试,只需将您的 id 属性重命名为 contactId 并创建 getters 和 setter,相应地修改 Contact.hbm.xml 并查看 hibernate 是否抱怨无法为新的 getter 方法属性。感觉有一些项目清理问题
此致,
不要试图解决。将 id 列的名称更改为 "cid" 或 "contact_id" 或您想要的任何名称。