views:

658

answers:

1

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...
A: 
  1. This is for performance reasons (prefetching)
  2. Read the hibernate source code.
kazanaki