Results 1 to 4 of 4

Thread: Hibernate/Spring: Illegal attempt to associate a collection with two open sessions in

  1. #1

    Default Hibernate/Spring: Illegal attempt to associate a collection with two open sessions in

    Hi,

    I am seeing the above error in my unit tests, and am at a loss how to correct it.

    My unit test class extends AbstractTransactionalDataSourceSpringContextTests and performs the following:

    Code:
    	// Retrieve a RuleGroup
    	RuleGroup ruleGroupToUpdate = ruleGroupDao.findRuleGroup(a, b, c, d);
    	// Update an encapsulated Attribute
        // Save the RuleGroup
    	ruleGroupDao.updateRuleGroup(ruleGroupToUpdate);
    The following is a snippet of my RuleGroup mapping file. As you can see, a RuleGroup has encapsulated Rule and Attribute collections, both of which are lazy loaded by default:

    Code:
    <class name="a.b.c.d.RuleGroup" table="RULE_GROUP">
    		<id name="ruleGroupID" column="RULE_GRP_ID">
    			<generator class="increment"/>
    		</id>
    		<set name="rules" lazy="true" cascade="save-update" table=RULES">
    		 	<key column="RULE_GRP_ID" not-null="false"/>
    			<many-to-many unique="true" column="RULE_ID" class="a.b.c.d.Rule"/>
    		</set>
    		<set name="attributes" lazy="true" cascade="save-update" table="ATTRIBUTES">
    			<key property-ref="xrefID" column="XREF_ID" not-null="true"/>
    			<many-to-many unique="true" column="CONFIG_ATTR_ID" class="a.b.c.d.Attribute"/>				
    		</set>
    	</class>
    My DAO class extends HibernateDaoSupport:
    • The findRuleGroup method uses getSession().createCriteria() to perform the query. The rules and attributes are retrieved using a "join" fetch
    • The updateRuleGroup method uses getHibernateTemplate().update() to perform the update


    From debugging my test, a new Session is created in the findRuleGroup method, and then again in the updateRuleGroup method. Therefore, when the update is performed Hibernate sees that my Attribute collection is already associated with the findRuleGroup session and throws the exception.


    How do I resolve this? Presumably, the update should not open a new session. If I specify that HibernateTemplate should not create a new session, then I get a different exception saying there is no session associated with the thread. I am at a loss here!

  2. #2
    Join Date
    Aug 2006
    Location
    Arequipa-Peru / South America
    Posts
    2,796

    Default

    Could you post the complete error stack trace? without that is not easy to see the cause problem
    - Manuel Jordan

    Kill Your Pride, Share Your Knowledge With All
    The Fear Of The LORD Is The Beginning Of Knowledge, But Fools Despise Wisdom And Discipline. Proverbs 1:7

    Blog


    Technical Reviewer of Apress

    • Pro SpringSource dm Server
    • Spring Enterprise Recipes: A Problem-Solution Approach
    • Spring Recipes: A Problem-Solution Approach, 2nd Edition
    • Pro Spring Integration
    • Pro Spring Batch
    • Pro Spring 3
    • Pro Spring MVC: With Web Flow
    • Pro Spring Security

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    Make your test transactional, next to that you should really not use HibernateDaoSupport/HibernateTemplate as that should be considered deprecated (since about 2007).
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4

    Default

    Thanks for reply. I removed the deprecated AbstractTransactionalDataSourceSpringContextTests and switched to using Spring 3's annotation based unit testing.
    So once I did this, and annotated my test method with @Transactional the error no longer occurred.

Posting Permissions

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