org.hibernate.PropertyNotFoundException: Could not find a getter for id in class model.Contact

10,327

Here it is : Your configuration looks correct. Can do a simple test just rename your id attribute to say contactId and create getters and setters,modify Contact.hbm.xml accordingly and see if hibernate complains of not being able to getter method for the new attribute. Getting this feeling there is some project cleaning up issue

Regards,

Share:
10,327
ryvantage
Author by

ryvantage

Updated on July 16, 2022

Comments

  • ryvantage
    ryvantage almost 2 years

    I am trying to learn Hibernate and I am following this tutorial: http://www.visualcplusdotnet.com/javaopensource/javahibernateswingdesktopapp5.html except I am using my own sample database. I have reverse engineered the POJOs for my contact table which looks like this:

    enter image description here

    and Netbeans auto-generated a .java file that looked like this (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;
        }
    
    
    
    
    }
    

    and a .xml file that looks like this (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>
    

    After a clean and build, when I run this HQL Query:

    from Contact
    

    I get this error:

    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)
    

    after reading the answers to these questions:

    I figured the answer must be something wrong with the way id is being capitalized by the getter, so I tried:

    public Integer getid() {
        return this.id;
    }
    
    public int getId() {
        return this.id;
    }
    
    public int getid() {
        return this.id;
    }
    

    All kinds of things, but still nothing works.

    Does anyone know why I'm receiving this error?

    • jithin iyyani
      jithin iyyani about 9 years
      Your configuration looks correct. Can do a simple test just rename your id attribute to say contactId and create getters and setters,modify Contact.hbm.xml accordingly and see if hibernate complains of not being able to getter method for the new attribute. Getting this feeling there is some project cleaning up issue.
    • ryvantage
      ryvantage about 9 years
      @jitsonfire, that, indeed, solved the problem. I've always had the practice of naming the auto-generated primary key for each table as "id." I guess that practice is over...
    • ryvantage
      ryvantage about 9 years
      If you leave your comment as an answer I'll accept it.
    • jithin iyyani
      jithin iyyani about 9 years
      have added it as an answer :)
  • ryvantage
    ryvantage about 9 years
    This worked. id is, apparently`, not a good name for the primary key.