1 2 Previous Next 27 Replies Latest reply on Apr 4, 2012 11:27 AM by newway

    AS7 has wrong behavior for @GeneratedValue(AUTO)


      I'm a bit surprised by how AS7.0.0.Final and AS7.1.0-SNAPSHOT (build #1477) generates IDs for @GeneratedValue(strategy = GenerationType.AUTO) on persistent entities. It actually increments IDs accross tables/entities.


      Example using ExampleDS and standard standalone configuration:



      public abstract class AbstractEntity implements Serializable {
           protected Long id;     
           @GeneratedValue(strategy = GenerationType.AUTO)
           public Long getId() {
                return id;
           public void setId(Long id) {
                this.id = id;




      public class Part extends AbstractEntity {




      public class Piece extends AbstractEntity {



      When I persist one instance of Part and then an instance of Piece.


      Part will be given ID=1

      Piece will be given ID=2


      This must be wrong as both entities should have an ID of 1 for each initial instance.


      I could not find any bug report on this, so I start suspect I'm doing something wrong here.

        • 1. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

          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.

          1 of 1 people found this helpful
          • 2. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

            How come H2 in AS6 did not behave in the same way as for AS7?


            I'll dig through HHH.


            Thanks for your clarification.

            • 3. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

              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.

              • 4. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                Hi Ove,


                AS7 comes with Hibernate 4.

                So it's maybe a change in Hibernate 4 GeneratedValue behavior.




                • 5. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                  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




                  • 6. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                    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.

                    • 7. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                      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.




                      • 8. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                        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:




                        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.

                        • 9. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                          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).




                          • 10. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                            Hi Ove,


                            I think the right strategy is IDENTITY because MySQL supports auto_increment:



                            Regards, Simon

                            • 11. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                              Hi Simon,


                              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?




                              • 12. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                                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.

                                • 13. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                                  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.

                                  • 14. Re: AS7 has wrong behavior for @GeneratedValue(AUTO)

                                    Hi Ove,


                                    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.


                                    Regards, Simon

                                    1 2 Previous Next