-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: session().merge / multi form application transaction
PostPosted: Wed Mar 30, 2005 4:59 pm 
Newbie

Joined: Thu Dec 16, 2004 12:27 pm
Posts: 7
Location: Long Island
ex.

Multipart web update form where user makes edits to a 'Manuscript' on a main page. Can click to edit contact information on a second page. Changes to contact information on second page can be saved or canceled which returns to the first page. No updates should go to database until 'save' on main page is done.

Can hibernate help with this scenario? I was attempting to use session.merge() to make a temporary copy of the object graph in a second session which is manipulated in the sub-form. If the user commits, the changes are merged back to the 'Manuscript' in the main session, if the user cancels, the temporary object graph is thrown away.
This fails however if the user adds new contact information with the 'reassociated object has dirty collection' error. The alternative, I can think of is to backup the object graph myself using commons-beanutils and manually revert the object graph is the 'sub form' is canceled. I thought the merge solution would be cleaner. Is there a standard pattern to achieve this behaviour?

Right now I'm using spring's OpenSessionInView filter and I reassociate my top level 'Manuscript' object at the beginning of each request. Below is a small code snippet that simulates the mult-request scenario.

-lenny

Version
3.0rc1

Mapping documents:

<class
name="Manuscript"
table="MANUSCRIPT" schema="MANUSCRIPTS"
>

<id name="manuscriptId" type="java.lang.Long"
column="MANUSCRIPT_ID">
<generator class="sequence">
<param name="sequence">MANUSCRIPT_SEQ</param>
<param name="schema">MANUSCRIPTS</param>
</generator>
</id>

<!-- bi-directional one-to-many association to ManContactInfo -->
<bag
name="manContactInfos"
lazy="true"
cascade="all,delete-orphan"
inverse="true"
>
<key>
<column name="MANUSCRIPT_ID" />
</key>
<one-to-many
class="ManContactInfo"
/>
</bag>
....
Code between sessionFactory.openSession() and session.close():

public void testWithMerge() {

System.out.println("request1");

Session session1 = sessionFactory().openSession();
m = (Manuscript)session1
.createQuery("from Manuscript m " +
" join fetch m.manContactInfos " +
" where accode = 'CNR802'")
.uniqueResult();

Session session2 = sessionFactory().openSession();

Manuscript copy = (Manuscript)session2.merge(m);

session1.close();
session2.close();

System.out.println("request2");

Session session3 = sessionFactory().openSession();
session3.lock(m, LockMode.NONE);

System.out.println("changing");

ManContactInfo cinfo = new ManContactInfo();
copy.addToManContactInfosRelationship(cinfo);

System.out.println("contacts: " + m.getManContactInfos().size());
System.out.println("copy contacts: " + copy.getManContactInfos().size());

System.out.println("merging back");
session3.merge(copy);

System.out.println("contacts: " + m.getManContactInfos().size());

session3.close();

System.out.println("request3");

Session session4 = sessionFactory().openSession();
session4.lock(m, LockMode.NONE);

Transaction tx = session4.beginTransaction();
session4.update(m);
tx.commit();
session4.close();
}

Full stack trace of any exception that occurs:
org.hibernate.HibernateException: reassociated object has dirty collection
at org.hibernate.event.def.OnLockVisitor.processCollection(OnLockVisitor.java:46)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:81)
at org.hibernate.event.def.AbstractVisitor.processValues(AbstractVisitor.java:43)
at org.hibernate.event.def.AbstractVisitor.process(AbstractVisitor.java:106)
at org.hibernate.event.def.AbstractReassociateEventListener.reassociate(AbstractReassociateEventListener.java:75)
at org.hibernate.event.def.DefaultLockEventListener.onLock(DefaultLockEventListener.java:57)
at org.hibernate.impl.SessionImpl.lock(SessionImpl.java:456)
at org.aps.eop.dao.hibernate.ManuscriptDAOImplTest.testWithMerge(ManuscriptDAOImplTest.java:210)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:421)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:305)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:186)



Name and version of the database you are using:

Oracle9I

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:

request1
request2
changing
contacts: 1
copy contacts: 2
merging back
Hibernate: select MAN_CONTACT_INFO2_SEQ.nextval from dual
contacts: 2
request3

<exception here>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 31, 2005 3:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
1st solution (detached object):
Step 1: load the object graph you need and close the session

Step 2 (httpRequest 1): the detached object graph may have been updated --> NO PROBLEM, no need to re attach it

Step 3 (httpRequest 2): again the detached object graph may have been updated --> NO PROBLEM, no need to re attach it

Step 4 (httpRequest 3 validation): here just call session.merge(detached root object)

you need to have cascade activated to easily handle this situation and step 1 is important, otherwise you'll need to lock to initialise proxys

2nd solution (long session):
Step 1: load the object graph or root instance (maybe with usefull association uninitialized, nomatter), then you have to disconnect/reconnect at each httpRequest + setFlushMode to NONE

Step 2 (httpRequest 1): the object is still attached, may have been updated but nothing is going to be flushed because of FlushMode

Step 3 (httpRequest 2): the object is still attached, may have been updated but nothing is going to be flushed because of FlushMode

Step 4 (httpRequest 3 validation): here just call session.flush() + commit

This one requires a ServletFilter to manage disconnection/reconnection

So get Hibernate In Action, you'll find usefull things about this.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.