users@glassfish.java.net

Re: diferent behaviour between GeneratedType.AUTO and GeneratedType.SEQUENC

From: <glassfish_at_javadesktop.org>
Date: Tue, 19 Feb 2008 13:41:12 PST

This question was sent to the peristence mailing list as well where an answer was posted by James Sutherland. For the benefit of other users I will post that edited response here as well:

What database are you using? My guess is that you have either set IDENTITY or are using SEQUENCE on a database that does not support it, so IDENTITY ends up getting used.
The issue is that for IDENTITY the id is assigned by the database on the insert, so it not
available on the persist call. You must first call flush() to ensure that the id has been assigned.

In general with JPA persist does not ensure the id is assigned, it only must be assigned on flush, so if you wish to write generic code, you would need to flush. However if you use TABLE generated values you can be sure that it will be assigned on persist.

see,
http://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Common_Problems_2
[Message sent by forum member 'gyorke' (gyorke)]

http://forums.java.net/jive/thread.jspa?messageID=259761