How are managed the sequences by JPA and Hibernate?

Posted by romaintaz on Stack Overflow See other posts from Stack Overflow or by romaintaz
Published on 2009-01-21T08:50:53Z Indexed on 2010/05/28 18:02 UTC
Read the original article Hit count: 176

Filed under:
|
|

Hi all,

I am using Hibernate in my project, and many of my entities use a sequence for their technical keys. For example:

@Entity
@Table(name = "T_MYENTITY")
@SequenceGenerator(name = "S_MYENTITY", sequenceName = "S_MYENTITY")
public class MyEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "S_MYENTITY")
    @Column(name = "MY_ENTITY_ID")
    private Long entityId;

    ...

}

I have two questions about the ID generated by Hibernate when a new object of this class is persisted:

  1. Why SequenceGenerator (from javax.persistence) has a default value of allocationSize set to 50 instead of 1? What are the interests of that?
  2. What is the default algorithm used by Hibernate to calculate the generated ID? It seems that Hibernate uses the value returned by the sequence hosted by my Oracle database, but then modify it before assigning it to my entity...

© Stack Overflow or respective owner

Related posts about hibernate

Related posts about jpa