-->
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.  [ 3 posts ] 
Author Message
 Post subject: *** Automatization ***
PostPosted: Mon Mar 05, 2007 10:44 am 
Newbie

Joined: Mon Mar 05, 2007 10:31 am
Posts: 1
I am beginer in Hibernate and I see often
----------------------------------------------------------------
Session ssn = getSession();
Transaction tsn = ssn.beginTransaction();
// operation
tsn.commit();
ssn.close();
-------------- OR---------------------------------------------
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
// operation
tx.commit();
em.close();
----------------------------------------------------------------
How can I write method WITHOUT getSession(), beginTransaction, tsn.commit(), ssn.close() ? I would like it (container, or I don't know) was responsible for this

THANKS :)))


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 1:11 pm 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
Even without getSession() anywhere, it's gonna be difficult. But without managing transactions, it's quite simple. For example, use a transactional proxy that will manage your tx demarcation. Spring provides one for instance (see TransactionProxyFactoryBean).

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Mar 05, 2007 2:41 pm 
Senior
Senior

Joined: Tue Jul 25, 2006 9:05 am
Posts: 163
Location: Stuttgart/Karlsruhe, Germany
Hi,

If you run from within an app server such as JBoss, you can get the EntityManager injected with a call like so:

Code:
@PersistenceContext(unitName = "<unit name>")
protected EntityManager em;

Then you can just use calls like em.persist(), em.merge(), em.flush() ... and so on. The container will manage the tranasaction for you.


Cheers,

Andy

_________________
Rules are only there to be broken


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