25

I'm usign @GeneratedValue(strategy = GenerationType.AUTO) to generate the ID on my entity.

I don't now how it works, but on my child table, generates ID values, that follow the parent sequence.

//parent table
@Entity
@Table (name = "parent")
public class Parent {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column (name = "id")
    private long id;


    @OneToMany (cascade = {CascadeType.ALL}, fetch = FetchType.LAZY)
    @JoinColumn (name = "parentId")
    @ForeignKey (name = "FKparent")
    private List<child> child;

}

//child table
@Entity
@Table (name = "child")
public class Child {

    @Id
    @GeneratedValue (strategy = GenerationType.AUTO)
    @Column (name = "id")
    private long id;
}

The inserted ID values on parent, updates the sequence. The inserted ID values on child, updates the sequence. On the next insert of parent, the sequence... uses values updated by child insertions...

This Annotations, aren't creating two sequences, only one. Is this correct/expected?

I inserted my entities with my DAO service only using entityManager.persist(parent);

Kingfisher Phuoc
  • 8,052
  • 9
  • 46
  • 86
Moli
  • 411
  • 2
  • 6
  • 12

3 Answers3

56

These Annotations are no creating two sequences, only one. Is this correct/expected?

That's the expected behavior. When using @GeneratedValue(strategy = GenerationType.AUTO), the JPA provider will pick an appropriate strategy for the particular database. In the case of Oracle, this will be SEQUENCE and, since you did not specify anything, Hibernate will use a single global sequence called hibernate_sequence.

Is this correct? Well, I don't know, it depends on your needs. Just in case, the default maximum value for an Oracle sequence is 1E+27, or 1,000,000,000,000,000,000,000,000,000. That's enough for many.

Now, it is possible to use GenerationType.AUTO and still control the name of the sequence when the database uses sequences:

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private Long id;
Pascal Thivent
  • 562,542
  • 136
  • 1,062
  • 1,124
  • 6
    Pascal, but if you want to use GenerationType.AUTO to allow other databases like MySQL to use Identity Insert this will not work, won't it? Because you manually specify a generator – lujop Jan 11 '13 at 11:21
1

Yes this is correct and expected.

You can create individual sequences for each table, but IMHO that is just extra code with no actual benefit.

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
  • Not if the end user sees that keys and is misled by it being "jumpy". And don't tell me that the key needs to hidden from the user, that is not always possible... – Monoman Feb 27 '12 at 18:54
  • 1
    There is no way of keeping the ids constant in time and monotone and without holes in the sequence, that doesn't kill scalability. Neither have I seen a useful definition of 'jumpy' and why users need unjumpy IDs. – Jens Schauder Feb 28 '12 at 06:48
1
@Entity
@Table(name = "table_seq")
@SequenceGenerator(name= "NAME_SEQUENCE", sequenceName = "SEQ_ID", initialValue=1, allocationSize = 1)
public class SeqEntity implements Serializable {
  private static final long serialVersionUID = 1L;

 @Id
 @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="NAME_SEQUENCE")
 private Long id;

}
Elton Sandré
  • 372
  • 3
  • 7