代码之家  ›  专栏  ›  技术社区  ›  ryvantage

org.ibernate.PropertyNotFoundException:在类模型中找不到id的getter。请联系

  •  3
  • ryvantage  · 技术社区  · 9 年前

    我正在尝试学习Hibernate,并遵循本教程: http://www.visualcplusdotnet.com/javaopensource/javahibernateswingdesktopapp5.html 除了我使用自己的示例数据库。我已经为我的 contact 表,如下所示:

    enter image description here

    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;
    }
    

    各种各样的事情,但仍然没有起作用。

    有人知道我为什么收到这个错误吗?

    2 回复  |  直到 7 年前
        1
  •  3
  •   jithin iyyani    9 年前

    这里是: 您的配置看起来正确。可以做一个简单的测试,只需将id属性重命名为contactId,并创建getter和setter,相应地修改Contact.hbm.xml,看看hibernate是否抱怨不能为新属性使用getter方法。有这样的感觉,有一些项目清理问题

    当做

        2
  •  0
  •   user1545636    9 年前

    不要试图解决。将id列的名称更改为“cid”或“contact_id”或您想要的任何名称。