Results 1 to 5 of 5

Thread: Hibernate Integration testing problem

  1. #1
    Join Date
    Feb 2005
    Posts
    11

    Default 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("forget@about.it");
    		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. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    Feb 2005
    Posts
    11

    Default

    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?

  4. #4
    Join Date
    Jul 2006
    Posts
    1

    Default 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

  5. #5
    Join Date
    Aug 2011
    Posts
    1

    Default

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

    It's work's

Similar Threads

  1. Best practices for Hibernate integration testing
    By Ibexx in forum Architecture
    Replies: 3
    Last Post: Oct 19th, 2005, 01:38 AM
  2. Loosing my SecureContext
    By sklakken in forum Security
    Replies: 3
    Last Post: Jul 21st, 2005, 01:44 PM
  3. Replies: 1
    Last Post: Jul 5th, 2005, 03:48 AM
  4. Replies: 3
    Last Post: Nov 19th, 2004, 07:16 PM
  5. Problem using hibernate and unit testing
    By rodney.gallart in forum Data
    Replies: 5
    Last Post: Oct 25th, 2004, 12:02 PM

Posting Permissions

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