3

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;

}

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

how about oracle ? also how do you provide name of second sequence ?
0

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.

2 Comments

Not really! Let us know if you got any success on it.
No its not working,
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.