2011-11-28 71 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解决了这个问题