1

Hibernate supports a custom ID generator as noted in How to generate Custom Id in JPA

I was wondering, is there a JPA standard way of doing it?

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

1 Answers1

2

No, but you can write your own logic to assign primary key in a PrePersist lifecycle callback method.

This can further be expanded to use an EntityListener as noted in https://stackoverflow.com/a/54006736/242042 which allows you to inject Spring managed beans that your PrePersist operation would require if the EntityListener is managed by Spring.

If the EntityListener is not managed by Spring, ThreadLocal may be an alternative (rel: Is it safe to pass in the Spring Application Context into a ThreadLocal associated with a request?)

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
Andreas
  • 154,647
  • 11
  • 152
  • 247