0

I'm using 3 different JPA SequenceGenerators. Everyone creates its own table in datasource with given name:

@SequenceGenerator(name = "a_seq", sequenceName = "A_SEQ")
@SequenceGenerator(name = "b_seq", sequenceName = "B_SEQ")
@SequenceGenerator(name = "c_seq", sequenceName = "C_SEQ")

Is there a way to combine them all in one table, let's say SEQUENCE table, and every generator is one row in this table?

kaiser
  • 940
  • 1
  • 10
  • 25

2 Answers2

2

You need to use a Table generator (which stores the id value in a specified table) rather than Sequence generator (which uses native SQL sequences). This simplified example below should give you the idea, but you can control the schema for the table by specifying more attributes on the @TableGenerator annotation.

@TableGenerator(name="MyTableGen", table="SEQUENCES", pkColumnValue="MyClass")
@Entity
public class MyEntity
{
    @Id
    @GeneratedValue(strategy=GenerationType.TABLE, generator="MyTableGen")
    private long myId;

    ...
}
Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
0

like this. sample code.

@Entity(name = "project_document")
public class Document {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int idx;

    // ... setter, getter
}

...

@Id
@GeneratedValue(strategy=GenerationType.AUTO, generator="my_entity_seq_gen")
@SequenceGenerator(name="my_entity_seq_gen", sequenceName="MY_ENTITY_SEQ")
private long id;

enter link description here

Community
  • 1
  • 1
0gam
  • 1,343
  • 1
  • 9
  • 21
  • AUTO means that the JPA provider will decide for itself which value generation strategy to use. So you could get AUTO_INCREMENT, or SEQUENCE or something else. Hence this does not answer the question. – Neil Stockton Dec 16 '16 at 06:59
  • oh. Neil.. see you again. okay i agree. my sample code is not good answer. but last link good answer. – 0gam Dec 16 '16 at 07:04