I am having problems persisting associated objects to database
Here is the situation
- I have 2 associated objects viz. DeviceType and Role. That means DeviceType is associated with Role .
- Relationship is Uni-directional Many-to-Many
- Here are the POJOs
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:
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;
}
}
- Here is the mapping file
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>
- Persistence Snippet
Code:
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()); // Works fine
hibernateTemplate.save(dt); // Throws Exception shown below
}
- Stack Trace
Code:
[org.hibernate.property.BasicPropertyAccessor] - <IllegalArgumentException in class: test.Role, getter method of property: id>
<IllegalArgumentException occurred calling getter of test.Role.id; nested exception is org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of test.Role.id>
- An Example on how to persist associated objects in the persist method of XXXDAOHibernateImpl Class will be highly appreciated