Note that there are some explanatory texts on larger screens.

plurals
  1. POHibernate, @SequenceGenerator and allocationSize
    primarykey
    data
    text
    <p>We all know the default behaviour of Hibernate when using <code>@SequenceGenerator</code> - it increases real database sequence by <strong>one</strong>, multiple this value by 50 (default <code>allocationSize</code> value) - and then uses this value as entity ID.</p> <p>This is incorrect behaviour and conflicts with <a href="http://docs.oracle.com/javaee/5/api/javax/persistence/SequenceGenerator.html">specification</a> which says:</p> <blockquote> <p>allocationSize - (Optional) The amount to increment by when allocating sequence numbers from the sequence.</p> </blockquote> <p>To be clear: I do not bother about gaps between generated IDs.</p> <p>I care about IDs that are <strong>not consistent</strong> with underlying database sequence. For example: any other application (that e.g. uses plain JDBC) may want to insert new rows under IDs obtained from sequence - but all those values may be already used by Hibernate! Madness.</p> <p>Do somebody know any solution to this problem (without setting <code>allocationSize=1</code> and thus degrading performance)?</p> <p><strong>EDIT:</strong><br> To make things clear. If last inserted record had ID = <code>1</code>, then HB use values <code>51, 52, 53...</code> for its new entities BUT at the same time: sequence's value in database will be set to <code>2</code>. Which can easily leads to errors when other applications are using that sequence. <br/></p> <p>On the othe hand: specification says (in my understanding) that database sequence should have been set to <code>51</code> and in the meantime HB should use values from range <code>2, 3 ... 50</code></p> <p><br/><strong>UPDATE:</strong> <br/> As Steve Ebersole mentioned below: the behaviour described by me (and also the most intuitive for many) can be enabled by setting <code>hibernate.id.new_generator_mappings=true</code>.</p> <p>Thanks all of You.</p> <p><strong>UPDATE 2:</strong><br> For future readers, below you can find a working example.</p> <pre><code>@Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "USERS_SEQ") @SequenceGenerator(name = "USERS_SEQ", sequenceName = "SEQUENCE_USERS") private Long id; } </code></pre> <p>persistence.xml</p> <pre><code>&lt;persistence-unit name="testPU"&gt; &lt;properties&gt; &lt;property name="hibernate.id.new_generator_mappings" value="true" /&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload