-->
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: JPA: premature INSERTing on persist()
PostPosted: Fri Feb 05, 2010 12:15 pm 
Newbie

Joined: Mon Nov 23, 2009 3:47 pm
Posts: 4
I am observing a behaviour of Hibernate used as JPA provider which I cannot understand. Could you please help?

I have to import external data into existing and possibly new persistent entities. The code I wrote does the following (in a transaction using a transaction-scoped entity manager):

(1) Try to find an existing entity using the business key (taken from the external data).
(2) If no such entity exists, create one and call persist() on it. (My metaphor for perist() is: "mark for persisting".)
(3) In both cases we now have a managed entity. Update it using external data.

At the end of the transaction the changes are flushed and committed by Hibernate JPA using SQL/JDBC.

Now the thing I do not understand reveals if you turn on tracing: In the case of a new entity, Hibernate generates for (2) and (3) two SQL statements:

INSERT for (2),
UPDATE for (3).

But a new entity could be stored using only one INSERT statement at the end of the transaction, using the latest state of the entity object.

Why does it work not this way? Is there some rule in the JPA specification or is it just an implementation choice?


Top
 Profile  
 
 Post subject: Re: JPA: premature INSERTing on persist()
PostPosted: Mon Feb 08, 2010 10:27 am 
Newbie

Joined: Mon Nov 23, 2009 3:47 pm
Posts: 4
May be it's because the identity must not be null after a persist() and it's a database-generated-value identity?


Top
 Profile  
 
 Post subject: Re: JPA: premature INSERTing on persist()
PostPosted: Wed Feb 10, 2010 10:04 am 
Newbie

Joined: Mon Nov 23, 2009 3:47 pm
Posts: 4
JPA Javadoc says:

persist

void persist(Object entity)

Make an entity instance managed and persistent.

(Stress on "and".)

So we probably need a different/additional mechanism for a clean Unit of Work concept...


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.