2011-11-28 93 views
1

與會話異常同步數據庫狀態我有一個表豆爲:無法在休眠

public class Employee implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.SEQUENCE) 
    @Column(name = "employee_id", unique = true, nullable = false) 
    @Basic(fetch = FetchType.EAGER) 
    private long id; 
} 

,當我嘗試插入數據庫行的異常出現「無法與會話同步數據庫狀態」:

19658 [http-bio-8080-exec-2] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: null 
119658 [http-bio-8080-exec-2] ERROR org.hibernate.util.JDBCExceptionReporter - Batch entry 0 insert into employee (basic_salary, code, fk_department_id, email, first_name, ip_phone_extension, is_default, last_name, password, threshold, voice_model, voice_validate, employee_id) values (NULL, 222, NULL, [email protected], sdfasfd, 21312, 0, fdsafsad, 2, NULL, NULL, NULL, 10) was aborted. Call getNextException to see the cause. 
119658 [http-bio-8080-exec-2] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 0, SQLState: 23505 
119658 [http-bio-8080-exec-2] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: duplicate key value violates unique constraint "employee_pkey" 
    Detail: Key (employee_id)=(10) already exists. 
119658 [http-bio-8080-exec-2] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database state with session 
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update 
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:96) 
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) 
    at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263) 
    at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:179) 
    at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) 
    at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:51) 
    at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1206) 
    at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:375) 
    at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137) 
    at org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:656) 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754) 
    at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723) 
    at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393) 
    at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120) 
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) 
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202) 
    at $Proxy35.addEmployee(Unknown Source) 
    at com.xeno.phoneSuite.beans.EmployeeBean.addOrUpdateEmployee(EmployeeBean.java:256) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 
    at java.lang.reflect.Method.invoke(Method.java:597) 
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262) 
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) 

爲什麼這個異常出現,我怎麼能解決它? 注:我使用的SQL腳本將數據添加到數據庫

+0

沒有足夠的信息/代碼。 –

回答

2

日誌中寫道:

119658 [http-bio-8080-exec-2] ERROR org.hibernate.util.JDBCExceptionReporter - ERROR: duplicate key value violates unique constraint "employee_pkey" Detail: Key (employee_id)=(10) already exists.

ID必須是唯一的,但你試圖創建已經存在於數據庫中的ID新員工。 id生成器的工作是確保這種事情不會發生。

Hibernate文檔writes

SEQUENCE (called seqhilo in Hibernate): uses a hi/lo algorithm to efficiently generate identifiers of type long, short or int, given a named database sequence.

的最常見的原因是發電機故障是,如果它的順序是不同步與實體表,例如由於實體是不通過Hibernate會(例如創建由一個SQL腳本),並且序列沒有更新。

+0

我真的通過sql腳本添加行如何更新序列 – abdelhady

+0

這取決於您使用的是哪個數據庫。檢查它的文檔。 – meriton

+0

我使用postgres sql – abdelhady

0

我覺得行:

Detail: Key (employee_id)=(10) already exists 

給它了。檢查數據庫的東西有:

select * from employee where employee_id=10 
+0

我創建sql腳本插入數據庫和id 10存在 – abdelhady

0

我通過GenerationType.IDENTITY

public class Employee implements Serializable { 

    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY) 
    @Column(name = "employee_id", unique = true, nullable = false) 
    @Basic(fetch = FetchType.EAGER) 
    private long id; 
} 

,這將使每個表順序和SQL腳本,我們不是在INSERT語句中添加的ID解決了這個問題