Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Integration testing problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Integration testing problem

    Hi.

    I am having a problem with integration testing w/ Hibernate and Spring. During multiple calls (see code snippits below) in a test case Hibernate will complain with this error:

    Code:
    org.springframework.orm.hibernate3.HibernateSystemException: a different object with the same identifier value was already associated with the session: [com.tabarca.domain.User#71]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.tabarca.domain.User#71]
    org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [com.tabarca.domain.User#71]
    	at org.hibernate.engine.PersistenceContext.checkUniqueness(PersistenceContext.java:586)
    	at org.hibernate.event.def.DefaultDeleteEventListener.onDelete(DefaultDeleteEventListener.java:72)
    	at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:579)
    	at org.springframework.orm.hibernate3.HibernateTemplate$25.doInHibernate(HibernateTemplate.java:750)
    	at org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:358)
    	at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:744)
    	at org.springframework.orm.hibernate3.HibernateTemplate.delete(HibernateTemplate.java:740)
    	at com.tabarca.dao.hibernate.GenericHibernateDAO.delete(GenericHibernateDAO.java:31)
    	at com.tabarca.domain.logic.TabarcaImpl.deleteUser(TabarcaImpl.java:47)
    	at ... [snipped for readability]

    I have tried to force hibernate to flush and evict after add/update/delete calls with no success. Any ideas?

    Here is the snip from the test case
    It dies on the line: this.bizImpl.deleteUser(user);

    Code:
    public abstract class AbstractUserIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests {
    
    	protected BizFacade bizImpl;
    	
    	public void setBizImpl(BizFacade bizImpl) {
    		this.bizImpl= bizImpl;
    	}
    
    	public void testDeleteUser() {
    		// Add user to database
    		User user = new User();
    		user.setFirstname("Blake");
    		user.setLastname("Tucker");
    		user.setUsername("btucker");
    		user.setPassword("tuckb989");
    		user.setEmail("[email protected]");
    		this.bizImpl.addUser(user);
    		
    		// Assert that user is saved to database
    		List<User> users = this.bizImpl.findAllUsers&#40;&#41;;
    		Assert.assertEquals&#40;"Incorrect number of users found", users.size&#40;&#41;, 1&#41;;			
    		
    		// Delete user
    		this.bizImpl.deleteUser&#40;user&#41;;
    		
    		// Assert that user has been deleted from database
    		users = this.bizImpl.findAllUsers&#40;&#41;;	
    		Assert.assertEquals&#40;"Incorrect number of users found", users.size&#40;&#41;, 0&#41;;	
    	&#125;	
    &#125;
    Here is the relevant Hibernate code snip

    Code:
    public class GenericHibernateDAO<T, ID extends Serializable> extends HibernateDaoSupport implements GenericEntityDAO<T, ID> &#123;
    
        private Class<T> persistentClass;
    
        public GenericHibernateDAO&#40;Class<T> persistentClass&#41; &#123;
            this.persistentClass = persistentClass;
        &#125;
        
    	public T add&#40;T entity&#41; throws DataAccessException &#123;
    		this.getHibernateTemplate&#40;&#41;.merge&#40;entity&#41;;
            return entity;
    	&#125;
    &#125;
    Any help would be greatly appreciated.

    Thanks,
    Ibexx

  • #2
    This is the line that throws the exception considering the stacktrace:

    Code:
     // Delete user
          this.bizImpl.deleteUser&#40;user&#41;;
    This happens because after retrieving the user list, you associate the entities with the session and the user you create si already associated.

    Comment


    • #3
      Ok... so does anyone know how to test this very simple senario w/ Hibernate of adding/finding/deleting an entity. i have tried flushing and evicting between calls like this:

      Code:
      HibernateUserDAO hibernateUserDAO = &#40;HibernateUserDAO&#41;applicationContext.getBean&#40;"userDAO"&#41;;
      hibernateUserDAO.getHibernateTemplate&#40;&#41;.flush&#40;&#41;;
      
      SessionFactory sessionFactory = hibernateUserDAO.getSessionFactory&#40;&#41;;
      sessionFactory.evict&#40;User.class&#41;;
      ... and nothing has seemed to work.

      Any ideas on how to fix this?

      Comment


      • #4
        Maybe this could help

        I had the same problem. Maybe this could be useful for you.

        http://saloon.javaranch.com/cgi-bin/...&f=78&t=001363

        Success!

        /Jennya

        Comment


        • #5
          use this.getHibernateTemplate().get(Clazz.class, Serializable id) to read and this.getSession().saveOrUpdate().

          It's work's

          Comment

          Working...
          X