20

I am working on a product that will be supporting multiple database engines (Oracle, MSSQL, MySQL). For Oracle, I would prefer to use Sequences rather than a Sequence table to avoid potential concurrency and locking issues on a high-volume installation, but other database engines do not support sequences. Furthermore, I would prefer to use one sequence per table rather than a global sequence (such as hibernate_sequence), so @GeneratedValue(strategy = GenerationType.AUTO) won't work. Is there a way to dynamically choose the strategy at runtime?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Clay
  • 2,932
  • 2
  • 18
  • 16

1 Answers1

39

Actually, Hibernate interprets both GenerationType.AUTO and GenerationType.SEQUENCE using its org.hibernate.id.enhanced.SequenceStyleGenerator. SequenceStyleGenerator is an id generation strategy that picks one of two strategies based on what the underlying database supports. If the database supports sequences, SequenceStyleGenerator uses sequences; if it does not, SequenceStyleGenerator falls back to using a "sequence table". This "mapping" of which generator to use is controlled by a setting: hibernate.id.new_generator_mappings. Setting that to true enables the behavior I just described above. Unfortunately, for backwards compatibility reasons, we had to default that to false. So to take advantage of that, you'll need to make sure that setting is set to true.

Further, you can configure SequenceStyleGenerator to prefer either a global sequence or sequence per entity if no name is given. This is controlled by a setting named prefer_sequence_per_entity

SequenceStyleGenerator is quite configurable in general. Have a look at its javadocs for more information: http://docs.jboss.org/hibernate/orm/4.1/javadocs/index.html?org/hibernate/id/enhanced/SequenceStyleGenerator.html

Steve Ebersole
  • 9,339
  • 2
  • 48
  • 46
  • how is it configurable, I'm yet to find one example of how to change the increment_size of SequenceStyleGenerator ? the params passed to configure method are limited set of the properties set in the cfg.xml. optimizer and increment_size are not passed to the best of my efforts. – Eyad Ebrahim Nov 13 '13 at 16:47
  • 2
    `@SequenceGenerator( ..., allocationSize=blah )` – Steve Ebersole Nov 13 '13 at 21:26
  • Thanks for the suggestion. But it seems like a shortcut, since SequenceGenerator does not have the same properties of configuration mentioned in SequenceStyleGenerator. I tried this and it worked with Pooled Optimizer and it gave me the performance edge i was expecting. But for the sake of closure. Is there a way to do that in the cfg.xml file. The "optimizer" and "increment_size" on the SequenceStyleGenerator.java were not read by hibernate when I was debugging it. http://stackoverflow.com/questions/19694154/hibernate-cfg-xml-configuration-for-sequence-generator – Eyad Ebrahim Nov 14 '13 at 12:28
  • Since 'cfg.xml' names configuration settings (not mappings) not sure how you would expect to have this configurable via 'cfg.xml'. Perhaps you mean 'hbm.xml'? – Steve Ebersole Nov 18 '13 at 18:55
  • i was confusing lots of things. But on the plus side, now I get how those generators work and how to configure one properly. Thanks a lot (I inherited the code annotated, always assumed that the cfg.xml would have had the mapping otherwise). – Eyad Ebrahim Nov 19 '13 at 22:05
  • 1
    How can you pass in the `prefer_sequence_per_entity` parameter to `SequenceStyleGenerator` when using just `strategy=SEQUENCE` + `@SequenceGenerator`? Is there anything shorter than falling back to `@GeneratedValue(generator="mygen")` and then `@GenericGenerator(name="mygen", strategy="org.hibernate.id.enhanced.SequenceStyleGenerator", parameters=...)`? – Marko Topolnik Mar 28 '14 at 14:45
  • Will `SequenceStyleGenerator` choose IDENTITY on SQL Server (cause it works with `GenerationType.AUTO`)? Could you provide some implementation ? – user2171669 Jan 29 '15 at 18:21
  • No, SequenceStyleGenerator always uses a sequence or a "sequence table". It never uses IDENTITY. As for "some implementation", you realize Hibernate is open-source right? ;) https://github.com/hibernate/hibernate-orm/blob/master/hibernate-core/src/main/java/org/hibernate/id/enhanced/SequenceStyleGenerator.java – Steve Ebersole Jan 29 '15 at 20:02
  • 1
    Starting with Hibernate 5 "hibernate.id.new_generator_mappings" is set to true by default: http://docs.jboss.org/hibernate/orm/5.2/userguide/html_single/Hibernate_User_Guide.html – Kayvan Tehrani Jul 18 '18 at 05:24