Siddharth Agarwal
Siddharth Agarwal

Reputation: 135

Multiple SequenceGenerator in Hibernate Entity

Is it possible to use 2 sequence generators in Hibernate Entity Class. I want to use two sequence generator for my case one for the primary key and other for a simple field. How can i achieve the same?

@Data
@Table(name="a_b")
@SequenceGenerator(name = "a1_seq", sequenceName = "a1_seq", allocationSize    = 1)
@SequenceGenerator(name = "b1_seq", sequenceName = "b1_seq", allocationSize = 1)
public class ABC {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "a1_seq")
    private Integer id;

    @Column(name = "c")
    private String c;

    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "b1")
    @Column(name = "b)
    private Integer b;

}

Upvotes: 3

Views: 5608

Answers (3)

Gayath Halanayake
Gayath Halanayake

Reputation: 1

Trick is to add @Id annotation to both Sequence Generators. You can achieve this by using @IdClass().

@Entity
@Table(name = "table_name")
@IdClass(DBSequenceId.class)
public class DBSequence implements Serializable {

    @Id
    @SequenceGenerator(name = "yourName1", sequenceName = "yourSeqName1", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "yourName1")
    @Column(name = "first_seq", updatable = false)
    protected int firstSeq;

    @Id
    @SequenceGenerator(name = "yourName2", sequenceName = "yourSeqName2", allocationSize = 1)
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "yourName2")
    @Column(name = "second_seq", updatable = false)
    protected int secondSeq;

}

class DBSequenceId implements Serializable {

    private int firstSeq;
    private int secondSeq;

    //Setters and getters are omitted 
}

Tested on MariaDB

Upvotes: 0

Vlad Mihalcea
Vlad Mihalcea

Reputation: 154090

You should have only one SequenceGenerator for the Primary Key:

@Id
@Column(name = "id")
@SequenceGenerator(name = "a1_seq", sequenceName = "a1_seq", allocationSize    = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "a1_seq")
private Integer id;

and for the foreign key you could have:

@Column(name = "b, columnDefinition="serial")
private Integer b;

which should work for PostgreSQL.

Upvotes: 1

user1560185
user1560185

Reputation: 1

Define your @SequenceGenerator at column level rather than at class level. So you can create two separate sequenceGenerator - a1_seq and b1 for two different columns.

@Data
@Table(name="a_b")

public class ABC {

  @Id
  @Column(name = "id")
  @SequenceGenerator(name = "a1_seq", sequenceName = "a1_seq", allocationSize    = 1)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "a1_seq")
  private Integer id;

  @Column(name = "c")
  private String c;

  @SequenceGenerator(name = "b1_seq", sequenceName = "b1_seq", allocationSize = 1)
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "b1")
  @Column(name = "b)
  private Integer b;

}

This should work. I have not tested it but since SequenceGenerator is allowed at field level, it should work.

Upvotes: 0

Related Questions