[英]Could not synchronize database state with session Exception in hibernate
我有一個表bean:
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, admi222222222n@xeno-solutions.com, 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腳本將數據添加到數據庫中
日志寫道:
119658 [http-bio-8080-exec-2]錯誤org.hibernate.util.JDBCExceptionReporter - 錯誤:重復鍵值違反唯一約束“employee_pkey”詳細信息:鍵(employee_id)=(10)已存在。
ID必須是唯一的,但是您嘗試使用數據庫中已經存在的ID創建新的Employee。 id生成器的工作是確保不會發生這樣的事情。
休眠文檔寫道 :
SEQUENCE(在Hibernate中稱為seqhilo):在給定命名數據庫序列的情況下,使用hi / lo算法有效地生成long,short或int類型的標識符。
該生成器失敗的最常見原因是它的序列與實體表不同步,例如因為實體是在沒有經過休眠(例如通過SQL腳本)的情況下創建的,並且序列未更新。
我認為這句話:
Detail: Key (employee_id)=(10) already exists
放棄它。 檢查數據庫中的內容:
select * from employee where employee_id=10
我通過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腳本中,我們不在插入語句中添加id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.