1 of 1 people found this helpful
The right place to search/report a bug report on this would be https://hibernate.onjira.com/browse/HHH.
I don't think its a bug though, as AUTO may not be table specific (it can be a SEQUENCE object shared across tables). The JPA specification, mentions that the persistence provider should decide how to handle AUTO, based on the capabilities of the database. If you find that one provider has a better way of handling AUTO, perhaps that could influence other providers to do the same (this information could be included in a bug report/enhancement request).
It might help you to choose a different GenerationType choice that better matches your expectation.
I hope this helps.
How come H2 in AS6 did not behave in the same way as for AS7?
I'll dig through HHH.
Thanks for your clarification.
I agree, that it would be good to understand why your seeing a change in behaviour and if the change is expected. If you find an answer or not, please respond here.
AS7 comes with Hibernate 4.
So it's maybe a change in Hibernate 4 GeneratedValue behavior.
It's not clear to me whether this is an AS7 issue or that is Hibernate 4.
I have posted this on the Hibernate forum too and hope someone in AS7 og Hibernate Team could shed light on this. In my test cases IDs are asserted for and if I change to cross-table sequencing, it'll break my test suite when targeting i.e. MySQL.
Hibernate posting: https://forum.hibernate.org/viewtopic.php?f=1&t=1012142&p=2447624#p2447624
AS7 is automatically passing the "hibernate.id.new_generator_mappings" setting (http://docs.jboss.org/hibernate/annotations/3.5/reference/en/html/ch01.html) when it creates the EntityManagerFactory (when deploying a persistence.xml).
In Hibernate, if new_generator_mappings is enabled:
- @GeneratedValue(AUTO) maps to org.hibernate.id.enhanced.SequenceStyleGenerator
- @GeneratedValue(TABLE) maps to org.hibernate.id.enhanced.TableGenerator
- @GeneratedValue(SEQUENCE) maps to org.hibernate.id.enhanced.SequenceStyleGenerator
In Hibernate, if new_generator_mappings is disabled:
- @GeneratedValue(AUTO) maps to Hibernate "native"
- @GeneratedValue(TABLE) maps to org.hibernate.id.MultipleHiLoPerTableGenerator
- @GeneratedValue(SEQUENCE) to Hibernate "seqhilo"
This doesn't answer your question but gives a little more information that we can use to get the answer.
if your still listening to this thread, could you create a JPA issue requesting that AS7 keep the "hibernate.id.new_generator_mappings" property value (if specified in persistence.xml), so you could try to use the earlier Hibernate native (based on the Hibernate dialect used) behaviour.
https://issues.jboss.org/browse/AS7 is the place to create jiras.
This mapping of javax.persistence.GenerationType values listed here is correct. I highly suggest not reverting to the legacy strategy mappings. We decided on this new path for a reason.
The behavior you are seeing is the way org.hibernate.id.enhanced.SequenceStyleGenerator is designed to work when no name is given. Essentially it uses a default named sequence named "hibernate_sequence". The expectation is that if you want a special sequence used you would name it:
@Id @GeneratedValue(AUTO) @SequenceGenerator(sequenceName="specific_sequence") ...
Obviously that will not work with the id being defined on a root super class.
We will have a discussion whether it makes sense to add a feature similar to what we did for org.hibernate.id.enhanced.TableGenerator to allow requesting that a unique pool of values be used for id value generation (org.hibernate.id.enhanced.TableGenerator#CONFIG_PREFER_SEGMENT_PER_ENTITY). Scott will initiate that discussion with a Hibernate JIRA and a post to the hibernate-dev mailing list.
But as is, this is working as intended.
Scott and Steve,
Thanks for you reply. This was very clarifying. I've gone production with a solution using
@GeneratedValue(AUTO) and needless to say. That wasn't smart at all. Now, I need to figure out a new sequenecing strategy and how to upgrade tables and entities, dedicating table-wise sequencing. I'm a bit stuck with how to deal with MySQL5 using InnoDB and what strategy should I choose.
Do you have any recommendation here? I hope not to mess up my current MySQL5 deployment. Would IDENTITY be the right way, or do I need to write my own SequenceGenerator (which I hope not to do).
But if I change ~60 tables that has cross-table ID sequences (Long) wtih data in them. What will happen to the IDENTITY sequencing in MySQL then?
TableA is has latest ID 560, whereas TableB got ID 561, TableC got ID 562 and so forth.
Will Table A, B and C trigger sequencing from the last ID per table?
Actually I completely recommend never ever using IDENTITY for identifier generation from java apps at all, especially from O/RM based apps; really any strategy that requires an INSERT be performed to know the id value.
Specifically, IDENTITY-based generation kills attempts to leverage "conversations". Maybe you do not leverage such a paradigm in you application(s). But just in general, I prefer to avoid IDENTITY. YMMV.
Given your current set up, yes, I would writing a subclass of org.hibernate.id.enhanced.SequenceStyleGenerator, overriding the org.hibernate.id.enhanced.SequenceStyleGenerator#determineSequenceName method and using that in your annotations. The properties passed in there contain the name of the entity to which the generator is attached. You can use that to determine the name of the sequence to use.
Have a look at the MySQL manual what happens when adding auto_increment:
It will create sequences per table and should automatically use the latests id + 1.