gpt4 book ai didi

java - 无法将数据库状态与 hibernate 中的 session 异常同步

转载 作者:行者123 更新时间:2023-11-29 06:06:42 24 4
gpt4 key购买 nike

我有一个表 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;
}

当我尝试在数据库中插入行时出现异常“无法将数据库状态与 session 同步”:

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] 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 脚本)的情况下创建的,并且序列未更新。

关于java - 无法将数据库状态与 hibernate 中的 session 异常同步,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8299378/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com