-->
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: Application assigned id's documentation
PostPosted: Tue Jan 20, 2009 3:33 pm 
Newbie

Joined: Mon Aug 11, 2008 2:59 pm
Posts: 11
Hi, all.

We are using Hibernate 3.3.2. For various reasons, we're transitioning to application-assigned id's in our app. We use Hibernate as the JPA provider, marking our entities with JPA annotations. What we are seeing with application assigned id's is not matching with the documentation. We actually like what Hibernate appears to be doing rather than what the documentation says will happen, but we're confused as to why things are working as we want rather than as the documentation states. :) Specifically:

http://www.hibernate.org/hib_docs/refer ... d-assigned states "Choosing the assigned generator makes Hibernate use unsaved-value="undefined", forcing Hibernate to go to the database to determine if an instance is transient or detached, unless there is a version or timestamp property, or you define Interceptor.isUnsaved()." We have not explicitly chosen "assigned" for the id generator, but my understanding is that by having the entity's member variable marked as @Id, but no @GeneratedValue annotation is how you specify application assigned id's in JPA. This is what we've done (and we are assigning our identifiers and they are being used), but we (to our surprise, but thankfulness) find that no queries are being done to figure out if it's new or not when we hand new entities to the persist() method and then flush() the EntityManager (we have not implemented any kind of interceptor, marked anything as an unsaved-value of any kind, or have any attributes marked as @Version).

- and -

http://www.hibernate.org/hib_docs/entit ... ml#d0e1000 states "Exception: entity instances using application-assigned identifiers are inserted when they are saved." However, when we call EntityManager.persist() on our entities, it does not issue the SQL at that point, but waits until one of the other times a flush() occurs. I know EntityManager.persist() is probably not identical to Session.save(), so that might be the reason?

While what we're seeing is what we want to happen, it's not what the documentation appears to say will happen. We'd like to know if what we're seeing is expected behavior and we can rely on things working this way, or if things are behaving this way by happenstance and we shouldn't rely on this behavior continuing in future releases.

Thanks in advance,
Greg


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 20, 2009 5:34 pm 
Expert
Expert

Joined: Wed Mar 03, 2004 6:35 am
Posts: 1240
Location: Lund, Sweden
Regarding the first issue I think Hibernate only goes to the database if Session.saveOrUpdate() is called. This is the only method that uses the unsaved-value attribute and there is almost never any need to use that method.

Quote:
Exception: entity instances using application-assigned identifiers are inserted when they are saved.


This seems a bit odd. Until now I have had the impression that the only time an insert is immediately executed is if you call Session.save() and are using a generator that depends on the database for generating the id. One of the reasons for this is that the Session.save() method must return the ID of the object being save. It may be that the documentation is incorrect, but I have no experience with JPA/EntityManager so I can't say for sure.


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.