Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp
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