-->
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.  [ 12 posts ] 
Author Message
 Post subject: Collection initialization and session.flush()
PostPosted: Mon Dec 27, 2004 8:47 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
I hava class with set of another entity class as members e.g:
Code:
<class name="Item">
....
<set name="bids" table="BIDS" lazy="true" inverse="true" cascade="save-update">
<key column="ITEM_ID"/>

<many-to-many class="Bid column="BID_ID" outer-join="auto"/>
</set>
....


I persist newly instantiated class:
Code:
Item item = new Item();
item.setBids(new HashSet());
session.save(item);

and can use persisted object later in another session after obtaining lock on object via session.lock()
but HibernateException: reassociated object has dirty
collection
is thrown

this never happend if i call session.flush() before closing session which saves object.

In "Hibernate in Action" stated "...most applications rarely
need to call flush() explicitly". Why i have to flush session after persisting object?


Top
 Profile  
 
 Post subject: Re: Collection initialization and session.flush()
PostPosted: Mon Dec 27, 2004 9:46 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
fuzebest wrote:
In "Hibernate in Action" stated "...most applications rarely
need to call flush() explicitly". Why i have to flush session after persisting object?

Actually we think that most applications use 1 open session at a time for a tx, so flush is implicitly called.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 9:58 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
but this is not true for in this case: SLSB + CMT (business logic)+ DAO (persistance)

bean method calls several DAO methods and container commiting tx after that.
but if first DAO call creates object, second - updates it...

I'm using Steve Ebersole's ThreadLocalSession which is fine for nested calls, but not appropriate for "parallel" ones


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 10:14 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
This pattern is safe for consecutive DAO calls (which is *not* parallel)

If your semantic is
Code:
openSession
DAO1
DAO2
  DAO2.1
DAO3
closeSession



Then it's fine, because there is no reason to call saveOrUpdate in such case on entities coming for daos

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 10:37 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
No semantic is:
Code:
DAO1
openSession1
closeSession1
DAO2
openSession2
  DAO2.1
   (use session2)
closeSession2
DAO3
openSession3
closeSession3


only business objects passed between DAO calls, not session.
so i have to call save/update at the ends of DAO methods

The better way is to use single session in all DAOs, but if i implicitly call openSession()/session.close() what the point to use CMT tx, all nice declarative features are gone?
I want bean methods be clean of this low level persistance stuff as much as possible.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 10:48 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
fuzebest wrote:
No semantic is:
Code:
DAO1
openSession1
closeSession1
DAO2
openSession2
  DAO2.1
   (use session2)
closeSession2
DAO3
openSession3
closeSession3


only business objects passed between DAO calls, not session.
so i have to call save/update at the ends of DAO methods

The better way is to use single session in all DAOs, but if i implicitly call openSession()/session.close() what the point to use CMT tx, all nice declarative features are gone?
I want bean methods be clean of this low level persistance stuff as much as possible.

Usually, in this case, you should do
Code:
DAO1
openSession1
  tx = s.beginTransaction();
  tx.commit();
closeSession1
DAO2
openSession2
  tx = s.beginTransaction();
  DAO2.1
   (use session2)
  tx.commit();
closeSession2
DAO3
openSession3
  tx = s.beginTransaction();
  tx.commit();
closeSession3

Your code is then portable outside a container and the flush is called, the tx management is delegated to the CMT.
You can also explicitly call flush in the closeSession, if you don't want to use hibernate tx api.

Still, what you do is a bad practice (IMO), you lost a significant amount of power given by hibernate by not sharing the same session.

In my original case, You can use AOP, session filter, EJB generation or whatever to handle the openSession, closeSession declaratively.
I consider that session mgt and tx mgt are 2 different things, so I'm OK with having declarative tx and non declarative session mgt.

PS: you should have a look at the EJB3 spec for declarative session magagement.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 11:06 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
so hibernate tx manager delegate all transaction work to container if detects managet transactions?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 11:25 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Check the 3.5.5 chapter of the reference guide.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 1:02 pm 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
and what happend if i do not use explicitly hibernate tx manager?
i.e. do not
Code:
Transaction tx = session.beginTransaction();
...
tx.commit();


just rely on container managed transactions?
method calls still transaction guarded?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 27, 2004 1:46 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
The connection is involved in the tx just as JDBC.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 9:57 am 
Regular
Regular

Joined: Fri Dec 17, 2004 10:38 am
Posts: 54
emmanuel wrote:
The connection is involved in the tx just as JDBC.

pardon my misunderstanding but does this mean yes or no?
do i need use hibernate transaction manager with cmt transaction?


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 28, 2004 10:03 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Hibernate is not different from plain JDBC.
If you don't demark your tx on a CMT managed connection, the Tx is handled by the container. It is the same for hibernate.
Your method is then safe.

It is well explained in Hibernate in Action.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.