How to leave by default that hibernate does not interfere with the serial type Id of postgresql?

2

I have a field ID in PostgreSQL of type serial , to be auto-incremental. But when I do the mapping with Hibernate the .xml of the table is saved like this:

'<class name="Model.Persona" table="persona" schema="public" optimistic-lock="version">
    <id name="id" type="int">
        <column name="id" />
        <generator class="assigned" />
    </id></class>'

This means that when I want to send a record to the database it does not take into account the database autoincrementable and creates conflict.

    
asked by Xefiron 14.12.2018 в 22:02
source

1 answer

2

For those who have a similar problem, here is the solution that I have come to:

<class name="Model.Persona" table="persona" schema="public" optimistic-lock="version"> 
      <id name="id" type="int">
      <column name="id" />
      <generator class="sequence">
          <param name="sequence">persona_id_seq</param>
      </generator>
</id>

It consists of defining the generation class "assigned" to "sequence", once they define it as a sequence they enter as a parameter the name of the sequence that generates PostgreSql (The name is very intuitive, name of the Field_Table (id) _seq ).

In this way Hibernate will leave all the generation work of the Id to the database. I hope it helps someone else. Greetings.

    
answered by 18.12.2018 / 21:41
source