• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Unable to Insert Objects with Many-To-Many mapping

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.HibernateTemplate;
import org.springframework.dao.DataAccessException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ObjectRetrievalFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.orm.hibernate3.HibernateCallback;
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()+"***DeviceType="+dt.getDeviceType()+"***Desc="+dt.getDescription());
// hibernateTemplate.save(dt);
System.out.println("***ID="+dt.getId()+"***DeviceType="+dt.getDeviceType()+"***Desc="+dt.getDescription()+"***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
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please repost your mapping and java code, but use the CODE button below the Add Reply button so that the mapping and code keep their indentation so that it is easier to read.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Didn't you ask this question before, and maybe deleted our previous conversation.

Right now in your mapping I only see one side mapped for your many to many.

Which is device_types to Roles, but in your Roles mapping there is no mapping back to device_types. I expect to see one because you have inverse="true in the device_types mapping.

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your DeviceType and Roles objects, do you have a getter and setter for your "id" attribute? If not and only want Hibernate to access the attribute directly, you will need to set "access="field"" in your attributes for the id tag in the hibernate mapping file. Otherwise Hibernate wants to call a getter and setter method. Which when they aren't there causes an exception exactly like what you get.

Mark
reply
    Bookmark Topic Watch Topic
  • New Topic