Testing DAO with AbstractTransactionalDataSourceSpringContextTests
Hi all,
I am using Spring and Hibernate. I have written a DAO class which I would like to test.
So I created a class that extends AbstractTransactionalDataSourceSpringContextTests:
Code:
public class MyDaoTest extends AbstractTransactionalDataSourceSpringContextTests
{
private MyDao myDao;
private HibernateTemplate hibernateTemplate;
protected String[] getConfigLocations()
{
return new String[] {
"spring-common.xml",
"spring-data.xml" };
}
public void testGetMyItem()
{
// Insert some data into DB for testing
// According to the AbstractTransactionalDataSourceSpringContextTests,
// this data will be removed/rolld back from the DB after the test ends
User u = new User("myusername", "mypassword", "my.email@my.server");
UserItem g = new UserItem(u, "some data", "some data");
getHibernateTemplate().save(u);
getHibernateTemplate().save(g);
UserItem item = getMyDao().getUserItem(u.getId()); // In this line I am getting the exception
assertNotNull(item);
}
public MyDao getMyDao() {
return myDao;
}
public void setMyDao(MyDao myDao) {
this.myDao = myDao;
}
public HibernateTemplate getHibernateTemplate()
{
return hibernateTemplate;
}
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
{
this.hibernateTemplate = hibernateTemplate;
}
}
The hbm.xml is defined in the following way:
Code:
<class name="test.User" table="user">
<id name="id" type="long" unsaved-value="0">
<meta attribute="use-in-equals">true</meta>
<generator class="org.hibernate.id.enhanced.TableGenerator">
<param name="table_name">id_generator</param>
<param name="segment_value">user</param>
<param name="increment_size">100</param>
<param name="optimizer">pooled</param>
</generator>
</id>
<property name="username" unique="true" column="username" type="string" length="30" />
<property name="password" column="password" type="string" length="30"/>
<property name="email" column="email" type="string" length="150"/>
</class>
<class name="test.UserItem" table="user_item">
<id name="id" type="long" unsaved-value="0">
<meta attribute="use-in-equals">true</meta>
<generator class="org.hibernate.id.enhanced.TableGenerator">
<param name="table_name">id_generator</param>
<param name="segment_value">user_item</param>
<param name="increment_size">100</param>
<param name="optimizer">pooled</param>
</generator>
</id>
<many-to-one name="user" unique="true" column="user_id" property-ref="id" class="test.User" not-null="true" />
<property name="someData1" column="some_data_1" type="string" length="100" not-null="true"/>
<property name="someData2" column="some_data_2" type="string" length="50" not-null="true"/>
</class>
I am using hbm2java to generate Java classes from the hbm.xml file.
When I run the test I am getting the following error:
Code:
ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session
org.hibernate.HibernateException: Unable to resolve property: id
at org.hibernate.tuple.entity.EntityMetamodel.getPropertyIndex(EntityMetamodel.java:439)
at org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:286)
at org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:3586)
at org.hibernate.type.EntityType.getIdentifier(EntityType.java:404)
at org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:78)
at org.hibernate.persister.entity.AbstractEntityPersister.dehydrate(AbstractEntityPersister.java:1997)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2243)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2660)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:56)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:250)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:234)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultAutoFlushEventListener.onAutoFlush(DefaultAutoFlushEventListener.java:41)
at org.hibernate.impl.SessionImpl.autoFlushIfRequired(SessionImpl.java:969)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1114)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at org.springframework.orm.hibernate3.HibernateTemplate$30.doInHibernate(HibernateTemplate.java:875)
at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:372)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedParam(HibernateTemplate.java:866)
at org.springframework.orm.hibernate3.HibernateTemplate.findByNamedParam(HibernateTemplate.java:857)
Can anyone help me with this?
My understanding is that it has to do something with how AbstractTransactionalDataSourceSpringContextTests handles transactions.
But maybe I am wrong.
Any help would be appreciated.
Thanks