[hibernate-issues] [Hibernate-JIRA] Updated: (HHH-511) reattach object from same session

Rodrigo S. de Castro (JIRA) noreply at atlassian.com
Tue Aug 8 16:02:23 EDT 2006


     [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-511?page=all ]

Rodrigo S. de Castro updated HHH-511:
-------------------------------------

    Attachment: reattach-same-session.patch

Patch against Hibernate 3.1.3

Apply it with: patch -p1 -i ./reattach-same-session.patch

I developed this patch to fix two problems I faced when reattaching objects to the session after clearing it:

- "reassociated object has dirty collection reference"
- "Found two representations of same collection: (collection type)"

This patch fixes this problem by allowing an object from a given session to be reattached. To do that, there are two minor changes in two classes:

- OnLockVisitor: in processCollection(), when a collection has been attached to the same session, we only throw an exception if the collection is dirty, otherwise we reattach this collection to the session and move on.

- Collections: in processReachableCollection() method, when a given collection is not found in the session persistence context, we check if it is a collection that has been previously attached to the session. If it is, we add this collection back to the persistence context. That fixes "Found two representations..." problem.

This code was tested and works just fine for me, fixing the problems I was facing.  I hope this patch helps to solve the problems mentioned above.

PLEASE, check if this is the correct solution and review this code, in this case. I am not an expert in hibernate source code, so maybe there are other scenarios that reattaching objects is not allowed.

- Background

Some background of why I needed this problem fixed. Basically, I have a web application (spring mvc + hibernate) and wanted to develop JUnit tests for it. Using AbstractTransactionalSpringContextTests Spring class, each method is transactional, rolling back after its completion as a default behavior. 

However, my goal was simulate several requests in a given method, ie, within a transaction (which happens to be per method). For that, everything would be perfect if I could clear hibernate session, in order to test the scenario where I have LazyInitializationException and similar problems:

public void testMethod1() 
throws Exception {
// request1
request = new MockHttpServletRequest();
getCurrentSession().clear();
// .. request code ...

// request2
request = new MockHttpServletRequest();
getCurrentSession().clear();

// requestN
request = new MockHttpServletRequest();
getCurrentSession().clear();

}

But I had to implement thousands of workaround to avoid the problems mentioned above, what is impossible to maintain in the long run. 


> reattach object from same session
> ---------------------------------
>
>          Key: HHH-511
>          URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-511
>      Project: Hibernate3
>         Type: Bug

>   Components: core
>     Reporter: Gavin King
>     Priority: Minor
>  Attachments: reattach-same-session.patch
>
>
> http://forum.hibernate.org/viewtopic.php?p=2231400#2231400
> There is a problem when you reattach a collection to the same session that it was previously attached to, after calling clear(). Hibernate checks the collections session reference, at reattach and concludes it is  already attached.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
   http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see:
   http://www.atlassian.com/software/jira




More information about the hibernate-issues mailing list