Results 1 to 5 of 5

Thread: Testing DAO with AbstractTransactionalDataSourceSpringContextTests

  1. #1
    Join Date
    Jul 2007
    Posts
    13

    Exclamation 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

  2. #2
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Can you try to delete the "property-ref" from the many to one definition?

  3. #3
    Join Date
    Jul 2007
    Posts
    13

    Default

    Hi dejanp,

    Thanks a lot for a help.
    This has solved the issue.

    Can you please explain why was this "property-ref" causing the error?
    I thought that this attribute is used to define the name of a property of the associated class that is joined to this foreign key.

    The ref guide for the hibernate states the following:
    If not specified, the primary key of the associated class is used.
    And I was using exactly the primary key property in my previous hbm.xml definition, so I'm little puzzled why that didn't work and the mapping with default value works.

    Any thoughts are appreciated.

    Thanks

  4. #4
    Join Date
    Sep 2004
    Posts
    1,086

    Default

    Well, I can't explain it - I'd say it's a bug.

    On the other hand you can always argue that "id" is not a "property".

  5. #5
    Join Date
    Jul 2007
    Posts
    13

    Thumbs up

    Thanks Dejan, especially for the fastest responses I ever got (fast and correct of course ).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •