Hibernate and Postgresql autoincrement not syncing

dboshardy

I'm having a problem with Hibernate and Postgres. I'm repeating through a code block for testing purposes to make sure everything is working properly. This code block, however, includes repeat persist methods to persist two user accounts to the useraccount table. It works properly the first time. However, every subsequent time I run the code, the UserAccount objects IDs are incremented, while their corresponding users in the table stay at their original values. I've tried using native and identity as the generator class, identity keeps the id at 0 and native has the same issue I've described above.

I realize this is an unrealistic circumstance (repeating through an identical codeblock) but I don't feel that this is performing as it should. In fact, after removing all entries from all tables, the users are added to the user table properly. Is there a way to ensure that he object property id syncs with the table id?

Here is the xml mapping for the useraccount ids.

<class name="UserAccount" table="useraccounts">
    <id name = "UserId" type="int" column="user_id" unsaved-value="null">
        <generator class="increment"/>
    </id>

And here is the Hibernate config file

 <?xml version="1.0" encoding="utf-8"?>
 <!DOCTYPE hibernate-configuration SYSTEM
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 <hibernate-configuration>
 <session-factory>
    <property name="hibernate.dialect">
        org.hibernate.dialect.PostgreSQL82Dialect
    </property>
    <property name="hibernate.connection.driver_class">
        org.postgresql.Driver
    </property>
    <property name="hibernate.connection.url">
       jdbc:postgresql://localhost:5432/agora-db
    </property>
    <property name="hibernate.connection.username">
        postgres
    </property>
    <property name="hibernate.connection.password">
        postgres
    </property>
    <property name="connection_pool_size">1</property>
    <property name="hbm2ddl.auto">create</property>
    <property name="show_sql">true</property>
    <property name="hibernate.hbm2ddl.auto">update</property>
    <mapping resource="hibernate.hbm.xml"/>

 </session-factory>
 </hibernate-configuration>
Vlad Mihalcea

You should use a sequence identifier generator, because Postgres doesn't support IDENTITY columns natively. It supports SERIAL columns which emulate IDENTITY columns by using a sequence behind the scenes. Postgres supports sequences which are much more efficient than IDENTITY anyway.

So your id definition should look like this:

<id name="UserId" column="user_id">
    <generator class="sequence">
        <param name="sequence">USER_ID_SEQUENCE</param>
    </generator>
</id>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

PostgreSQL Autoincrement

From Dev

Syncing PouchDB offline to PostgreSQL

From Dev

PostgreSQL 9.1 primary key autoincrement

From Dev

PostgreSQL 9.1 primary key autoincrement

From Dev

autoincrement id null when inserting into postgresql table

From Dev

AutoIncrement Id PostgreSQL and Spring Boot Data JPA

From Dev

autoincrement id null when inserting into postgresql table

From Dev

Java Hibernate AutoIncrement Identity set ID to start 0 mySQL

From Dev

Postgresql, hibernate "is not distinct"

From Dev

Postgresql 'select distinct on' in hibernate

From Dev

Persisting EnumSet in Postgresql with Hibernate

From Dev

PostgreSQL network types in Hibernate

From Dev

Hibernate postgresql notify functionality

From Dev

Hibernate: PostgreSQL Driver issue

From Dev

Postgresql 'select distinct on' in hibernate

From Dev

Problems to configure hibernate and PostgreSQL

From Dev

How to connect hibernate with PostgreSQL in eclipse?

From Dev

Hibernate dealing with Date and Time - PostgreSql

From Dev

Postgresql JPA Hibernate create Database

From Dev

Spring Data JPA + Hibernate +PostgreSQL

From Dev

Hibernate Exception Missing table postgresql

From Dev

Setting Postgresql schema with hibernate parameters

From Dev

AUTOINCREMENT not working

From Dev

Insert with AutoIncrement

From Dev

JSON key search in PostgreSQL using Hibernate

From Dev

Multiple Hibernate sequence generators for one entity with PostgreSQL

From Dev

Use Point type with PostgreSQL and JPA/Hibernate

From Dev

Postgresql infinite loop after a hibernate criteria list

From Dev

Map refcursor of PostgreSQL to java entity using Hibernate

Related Related

HotTag

Archive