我遇到了Hibernate和Postgres的問題。我正在重複測試代碼塊以確保一切正常。但是,此代碼塊包含重複保留方法,以將兩個用戶帳戶保留到useraccount表中。它第一次正常工作。然而,每次我運行代碼時,UserAccount對象ID都會增加,而表中相應的用戶保持其原始值。我試過使用native
和identity
作爲生成器類,identity
保持id在0
和native
具有我上面描述的相同問題。休眠和Postgresql自動增量不同步
我意識到這是一個不切實際的情況(通過一個相同的代碼塊重複),但我不認爲這是表現得應該如此。實際上,在刪除所有表中的所有條目後,用戶將被正確添加到用戶表中。有沒有辦法確保他反對財產ID同步與表ID?
這裏是useraccount id的xml映射。
<class name="UserAccount" table="useraccounts">
<id name = "UserId" type="int" column="user_id" unsaved-value="null">
<generator class="increment"/>
</id>
這裏是Hibernate的配置文件
<?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>
注:這是genearaaly不使用的應用程序登錄Postgres超級用戶是一個好主意。更好地爲他們創建一個特殊的角色。 – joop 2014-11-17 11:34:38
有道理。我只是將它用於調試目的。謝謝! – dboshardy 2014-11-18 14:54:38