Wednesday 15 April 2015

java - Hibernate and Postgresql autoincrement not syncing -



java - Hibernate and Postgresql autoincrement not syncing -

i'm having problem hibernate , postgres. i'm repeating through code block testing purposes create sure working properly. code block, however, includes repeat persist methods persist 2 user accounts useraccount table. works first time. however, every subsequent time run code, useraccount objects ids incremented, while corresponding users in table remain @ original values. i've tried using native , identity generator class, identity keeps id @ 0 , native has same issue i've described above.

i realize unrealistic circumstance (repeating through identical codeblock) don't sense performing should. in fact, after removing entries tables, users added user table properly. there way ensure object property id syncs table id?

here xml mapping useraccount ids.

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

and here hibernate config file

<?xml version="1.0" encoding="utf-8"?> <!doctype hibernate-configuration scheme "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>

you should utilize sequence identifier generator, because postgres doesn't back upwards identity columns. postgres supports sequences much more efficient identity anyway.

so id definition should this:

<id name="userid" column="user_id"> <generator class="sequence"> <param name="sequence">user_id_sequence</param> </generator> </id>

java hibernate postgresql

No comments:

Post a Comment