Announcement Announcement Module
Collapse
No announcement yet.
IllegalArgumentException occurred calling getter method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • IllegalArgumentException occurred calling getter method

    Hibernate version: 3.1.2

    Mapping documents:
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

    <hibernate-mapping>
    <class name="test.DeviceType" table="DEVICE_TYPE">
    <id name="id" column="ID" unsaved-value="0" >
    <generator class="increment"/>
    </id>

    <property name="deviceType">
    <column name="NAME" />
    </property>

    <property name="description">
    <column name="DESC" />
    </property>

    <set name="roles" table="DEVICETYPE_ROLES" inverse="true" cascade="all">
    <key column="DEVICETYPE_ID" />
    <many-to-many column="ROLE_ID" class="test.Role"/>
    </set>

    </class>

    <class name="test.Role" table="ROLE">
    <id name="id" column="ID">
    <generator class="assigned">
    </generator>
    </id>

    <property name="name" column="NAME" />

    </class>



    </hibernate-mapping>




    Code between sessionFactory.openSession() and session.close():
    package test;

    import java.util.List;
    import org.springframework.orm.hibernate3.HibernateTempla te;
    import org.springframework.dao.DataAccessException;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.orm.ObjectRetrievalFailureExce ption;
    import org.springframework.orm.hibernate3.support.Hiberna teDaoSupport;
    import org.springframework.orm.hibernate3.HibernateCallba ck;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Query;
    import org.hibernate.SQLQuery;
    import org.springframework.dao.DataAccessException;
    import org.hibernate.HibernateException;



    public class DeviceDao {

    private HibernateTemplate hibernateTemplate;
    protected final Log logger = LogFactory.getLog(getClass());

    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
    logger.info(" *** In setHibernateTemplate method *** ");
    this.hibernateTemplate = hibernateTemplate;
    }
    public void persist(DeviceType dt) {
    logger.info(" *** In persist method *** ");
    System.out.println(" *** In persist method *** ");
    // System.out.println("***ID="+dt.getId()+"***DeviceT ype="+dt.getDeviceType()+"***Desc="+dt.getDescript ion());
    // hibernateTemplate.save(dt);
    System.out.println("***ID="+dt.getId()+"***DeviceT ype="+dt.getDeviceType()+"***Desc="+dt.getDescript ion()+"***Role Size="+dt.getRoles().size());
    hibernateTemplate.save(dt);
    System.out.println("ID***="+dt.getId());
    }
    public void update(DeviceType instance) {
    hibernateTemplate.update(instance);
    }
    public void remove(Long deviceTypeId) {
    Object device = hibernateTemplate.load(DeviceType.class, deviceTypeId);
    hibernateTemplate.delete(device);
    }



    public List getDeviceTypes() throws DataAccessException {

    return (List) hibernateTemplate.execute(new HibernateCallback() {
    public Object doInHibernate(Session session) throws HibernateException {
    String sql = "select dt.id, dt.deviceType from DeviceType dt";
    Query query = session.createQuery(sql);
    return query.list();
    }
    });

    }

    public DeviceType getDeviceType(Long id) {
    System.out.println("***In DeviceDao selectedDevice***"+id);
    DeviceType dt = (DeviceType) hibernateTemplate.get(DeviceType.class, id);
    if (dt == null) {
    throw new ObjectRetrievalFailureException(DeviceType.class, id);
    }
    return dt;
    }

    }




    Full stack trace of any exception that occurs:
    2007-01-19 12:14:03,759 ERROR [org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: test.Role, getter method of property: id>
    2007-01-19 12:14:03,759 WARN [test.TestBean] - <IllegalArgumentException occurred calling getter of test.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Role.id>



    Name and version of the database you are using:
    HSQLDB


    The generated SQL (show_sql=true):
    Expected to generate following SQL statements but it doesn't due to Error
    insert into DeviceType_Roles(DeviceType_id, Tole_Id) values (?, ?)
    insert into Device_Type(Id, Name, Desc) values(?,?,?)



    Debug level Hibernate log excerpt:
    2007-01-19 12:14:03,759 ERROR [org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: test.Role, getter method of property: id>
    2007-01-19 12:14:03,759 WARN [test.TestBean] - <IllegalArgumentException occurred calling getter of test.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Role.id>





    Problems with Session and transaction handling?
    Nope

    Scenario :
    Its an example of Many-To-Many Object Relationship i.e. DeviceType object is associated with Roles object. Here is the snippet
    DeviceType dt = new DeviceType();
    dt.setDeviceType(deviceType);
    dt.setDescription(description);
    dt.setRoles(deviceRoles);
    deviceDao.persist(dt); // In Business Layer
    Please not deviceRoles is Set of Roles.
    deviceDao.persist(dt); results in call to Hibernate Layer and executes hibernateTemplate.save(dt);

    Expected Results:
    To persist DeviceType object along with set of Roles into DeviceType table and DeviceType_Roles table as shown below
    insert into DeviceType_Roles(DeviceType_id, Tole_Id) values (?, ?)
    insert into Device_Type(Id, Name, Desc) values(?,?,?)

    Observed Results:
    IllegalArgumentException occurred calling getter method of property: id>

    Any pointers/suggestions will be highly appreciated

    Regards
    Bansi

  • #2
    Wrapping your code in [ code] [ /code] tags helps people read what your posting. Is it possible to see the full stacktrace?

    public class PropertyAccessException extends HibernateException

    A problem occurred accessing a property of an instance of a persistent class by reflection, or via CGLIB. There are a number of possible underlying causes, including

    failure of a security check
    an exception occurring inside the getter or setter method
    a nullable database column was mapped to a primitive-type property
    the Hibernate type was not castable to the property type (or vice-versa)
    http://www.hibernate.org/hib_docs/v3...Exception.html

    Comment


    • #3
      Hi karldmoore

      Thanks for reponse. Actually i am not getting the Exception directly when i hit the Submit button. I would came to know about it only thru Log4j properties file. Hence the complete stack trace is
      <code>
      2007-01-19 13:51:45,402 ERROR [org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: test.Role, getter method of property: id>
      2007-01-19 13:51:45,402 WARN [test.TestBean] - <IllegalArgumentException occurred calling getter of test.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Role.id>

      </code>

      Comment


      • #4
        Can I see the actual java for test.Role and test.DeviceType?

        Comment


        • #5
          Here you go ...

          package test;

          import java.util.Set;

          public class DeviceType {
          private Long id;
          private String deviceType;
          private String description;
          private Set roles;

          public DeviceType(){

          }

          public String getDescription() {
          return description;
          }
          public void setDescription(String description) {
          this.description = description;
          }
          public String getDeviceType() {
          return deviceType;
          }
          public void setDeviceType(String deviceType) {
          this.deviceType = deviceType;
          }
          public Long getId() {
          return id;
          }
          public void setId(Long id) {
          this.id = id;
          }

          public Set getRoles() {
          return roles;
          }

          public void setRoles(Set roles) {
          this.roles = roles;
          }


          }


          package test;

          public class Role {
          private Long id;
          private String name;

          public Long getId() {
          return id;
          }
          public void setId(Long id) {
          this.id = id;
          }
          public String getName() {
          return name;
          }
          public void setName(String name) {
          this.name = name;
          }


          }

          Comment


          • #6
            Ok, there are a few things here.......

            #1. Can you please put your code in [ code] [ /code] tags. Otherwise people can't read it and won't help!
            #2. Is it possible to see the full stacktrace, its hard to know what the problem is if you don't know where it's going wrong.
            #3. Role is using an 'assigned' ID generator, DeviceType is using 'increment'. Is that correct? You are generating and setting the ID for Role yourself?
            #4. I would really really recommend looking at the first Hibernate application example in the documentation. It is a great place to start.
            http://www.hibernate.org/hib_docs/v3...orial-firstapp
            #5. I would also really really recommend looking at the petclinic example that ships with Spring

            Comment


            • #7
              Thanks for response
              #1. Can you please put your code in [ code] [ /code] tags. Otherwise people can't read it and won't help!
              Here are the POJOs and the mapping file
              DeviceType.java
              [ code]
              package test;

              import java.util.Set;

              public class DeviceType {
              private Long id;
              private String deviceType;
              private String description;
              private Set roles;

              public DeviceType(){

              }

              public String getDescription() {
              return description;
              }
              public void setDescription(String description) {
              this.description = description;
              }
              public String getDeviceType() {
              return deviceType;
              }
              public void setDeviceType(String deviceType) {
              this.deviceType = deviceType;
              }
              public Long getId() {
              return id;
              }
              public void setId(Long id) {
              this.id = id;
              }

              public Set getRoles() {
              return roles;
              }

              public void setRoles(Set roles) {
              this.roles = roles;
              }


              }

              [ /code]

              [code]

              package test;

              public class Role {
              private Long id;
              private String name;

              public Long getId() {
              return id;
              }
              public void setId(Long id) {
              this.id = id;
              }
              public String getName() {
              return name;
              }
              public void setName(String name) {
              this.name = name;
              }


              }

              [ /code]
              Mapping File : deviceType.hbm.xml
              [ code]
              <?xml version="1.0"?>
              <!DOCTYPE hibernate-mapping PUBLIC
              "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
              "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

              <hibernate-mapping>
              <class name="test.DeviceType" table="DEVICE_TYPE">
              <id name="id" column="ID" unsaved-value="0" >
              <generator class="increment"/>
              </id>

              <property name="deviceType">
              <column name="NAME" />
              </property>

              <property name="description">
              <column name="DESC" />
              </property>

              <set name="roles" table="DEVICETYPE_ROLES" inverse="true" cascade="all">
              <key column="DEVICETYPE_ID" />
              <many-to-many column="ROLE_ID" class="test.Role"/>
              </set>

              </class>

              <class name="test.Role" table="ROLE">
              <id name="id" column="ID">
              <generator class="assigned">
              </generator>
              </id>

              <property name="name" column="NAME" />

              </class>
              </hibernate-mapping>
              [ /code]

              #2. Is it possible to see the full stacktrace, its hard to know what the problem is if you don't know where it's going wrong.
              Here is the full stack trace
              Code:
              2007-01-19 12:14:03,759 ERROR [org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: test.Role, getter method of property: id>
              2007-01-19 12:14:03,759 WARN [test.TestBean] - <IllegalArgumentException occurred calling getter of test.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Role.id>
              #3. Role is using an 'assigned' ID generator, DeviceType is using 'increment'. Is that correct? You are generating and setting the ID for Role yourself?
              Thats right. The Role has its ID values stored in the Database hence using "assigned" as the ID generator however in case of DeviceType whenever user submits the JSF form a new ID with an increment of 1 has to be inserted in the Database along with other field values
              #4. I would really really recommend looking at the first Hibernate application example in the documentation. It is a great place to start.
              http://www.hibernate.org/hib_docs/v3...orial-firstapp
              I will keep looking at Hibernate examples
              #5. I would also really really recommend looking at the petclinic example that ships with Spring
              Sure

              Comment

              Working...
              X