作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试学习 Hibernate 并且正在学习本教程:http://www.visualcplusdotnet.com/javaopensource/javahibernateswingdesktopapp5.html除了我使用自己的示例数据库。我已经为我的 contact
表逆向设计了 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)
阅读这些问题的答案后:
我想答案肯定是 id
被 getter 大写的方式有问题,所以我尝试了:
public Integer getid() {
return this.id;
}
public int getId() {
return this.id;
}
public int getid() {
return this.id;
}
各种各样的事情,但仍然没有任何效果。
有人知道我为什么会收到此错误吗?
最佳答案
这是:您的配置看起来正确。可以做一个简单的测试,只需将您的 id 属性重命名为 contactId 并创建 getter 和 setter,相应地修改 Contact.hbm.xml 并查看 hibernate 是否提示无法为新属性使用 getter 方法。感觉有一些项目清理问题
问候,
关于java - org.hibernate.PropertyNotFoundException : Could not find a getter for id in class model. 联系人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28892875/
我是一名优秀的程序员,十分优秀!