0

I've implemented a custom SequenceGenerator that I want to use in all my entities for the "id". But rather than having to do something like this for each entity:

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XyzIdGenerator")
@GenericGenerator(name = "XyzIdGenerator",
        strategy = "com.mycompany.myapp.id.BigIntegerSequenceGenerator",
        parameters = {
            @Parameter(name = "sequence", value = "xyz_id_sequence")
        })
public BigInteger getId()
{
   return id;
}

is there a way to apply this SequenceGenerator to ALL entities by default using vanilla Hibernate/JPA or perhaps by using Spring?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Johan
  • 37,479
  • 32
  • 149
  • 237
  • The best way to implement custom id generator using Annotation OR using XML Visit this link https://stackoverflow.com/a/50564556/9495226 – Sumit Anand May 28 '18 at 11:18

1 Answers1

1

Just move the code segment to a super class, add add @MappedSuperclass to it. But, in that case, all your entity will use the same seq generator

@MappedSuperclass
public class SeqIdable implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "XyzIdGenerator")
    @GenericGenerator(
    name = "XyzIdGenerator",
    strategy = "com.mycompany.myapp.id.BigIntegerSequenceGenerator",
    parameters = {
        @Parameter(name = "sequence", value = "xyz_id_sequence")
    })
    public BigInteger getId() {
       return id;
    }

}
NewBee
  • 1,331
  • 12
  • 13