skip to Main Content

I would very much appreciate your advice on the JPA. We have a database (PostgreSQL engine), and for tables it happens that a column has its DEFAULT. The database is connected to a Java backend (with JPA and Hibernate). What is the correct way to force JPA not to try to insert IDs for the primary keys that are generated by the table column DEFAULT?

I’ve tried the settings below, but unfortunately I’m having no luck:

enter image description here

Thank you in advance for any advice 🙂

I tried to search for a solution on the internet and in the documentation, but unfortunately, without success.

2

Answers


  1. Assuming that this is Hibernate 6 (you have not stated), the annotation you’re looking for is @Generated, and it’s documented here:

    https://docs.jboss.org/hibernate/orm/6.2/javadocs/org/hibernate/annotations/Generated.html

    I also tweeted an example almost exactly like yours here:

    https://twitter.com/1ovthafew/status/1645796021585457153

    So, something like:

        
    @Entity
    public class Entity {
            
        @Generated @Id
        @ColumnDefault("gen_random_uuid()")
        @Column(name = "id_assignment")
        UUID idAssignment;
    }
    

    should work.

    Login or Signup to reply.
  2. You should use the annotation @GenerateValue to let JPA not use the default generate strategy.

    The documentation is here: https://www.baeldung.com/hibernate-identifiers

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search