javaoracle-databasehibernatejpahilo

Disable hibernate HiLo Sequence generation


I'm working with Hibernate 4.2.x and I want to disable the HiLo sequence generation - go to the DB (oracle) every time. I added this row to the persistance.xml:

 <property name="hibernate.id.new_generator_mappings" value="true"/>

And my entity looks like this:

@Entity
@Table(name = "MY_TABLE")
@SequenceGenerator(name = "generator", sequenceName = "MY_SEQ", initialValue = 1, allocationSize = 1)
public class MyEntity {

    private long id;

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
    public Long getId()
    {
        return id;
    }
}

For some reason I still getting HiLo behavior - ids created far away from each other.

Looked on some questions (here and here for example), but didn't find anything helpful. More ever, I couldn't find where to configure which Optimizer to use.


Solution

  • Being not too familiar with hibernate, my guess is that it's using an Oracle database-sequence as source. One of the features of Oracle sequences is the sequence cache. check in the definition of the sequence if the cache size is set (default = 20). I know that after a restart of the database the cache is purged anyway, so that's when you lose consecutive numbers. Modify the sequence with the command: alter sequence MY_SEQ nocache;

    Keep in mind that OLAP performance may deteriorate when sequences are not cached.