gpt4 book ai didi

java - hibernate 断言失败: null id in entry (don't flush the Session after an exception occurs)

转载 作者:行者123 更新时间:2023-11-30 03:45:20 25 4
gpt4 key购买 nike

当抛出 org.hibernate.exception.ConstraintViolationException 时,AM 尝试在 catch block 中执行一些更新操作,如下所示

我使用 Hibernate 4.3.6

try {
services.entity.saveInstance(secQuestions)
}
catch(Exception e) {
if(e instanceof org.hibernate.exception.ConstraintViolationException){
userRegistrationRequest.requestProcessed = true
infoMsg = userRegistrationRequest.infoMessage+' Exception: '+e.message
userRegistrationRequest.infoMessage = infoMsg
entityService.update(userRegistrationRequest)
logger.info("Caught BaseApplicationException "+e.message)
return;
}

这是我的 UserRegistrationRequest 实体

@Audited
@javax.persistence.Entity
class UserRegistrationRequest implements IAmGroovy, Serializable, IVersionable {

@Autowired
protected transient Services services;
protected answerOne;
protected answerTwo;
protected emailAddress;
protected firstName;
protected infoMessage;
protected lastName;
protected requestExpired;
protected requestProcessed;
SecurityQuestion securityQuestionOne;
SecurityQuestion securityQuestionTwo;
UserSecurityQuestions securityQuestions;
}

这是我的 UserSecurityQuestions 实体

@Entity
@Audited
public class UserSecurityQuestions implements IPersistentEntityInstance {

private Long id;
private User user;
private SecurityQuestion question1;
private SecurityQuestion question2;
private String answer1;
private String answer2;
private String hashString;
private Timestamp validThrough;
private int tries;
}

尝试在 catch block 中执行更新时收到以下错误

    [Sep 17 11:48:14] ERROR | com.dc.core.common.controller.impl.BaseMultiActionController | null id in com.dc.apps.cp.security.model.UserSecurityQuestionsExt entry (don't flush the Session after an exception occurs) ERROR CODE(-1340973058)
org.hibernate.AssertionFailure: null id in com.dc.apps.cp.security.model.UserSecurityQuestionsExt entry (don't flush the Session after an exception occurs)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.checkId(DefaultFlushEntityEventListener.java:77)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.getValues(DefaultFlushEntityEventListener.java:192)
at org.hibernate.event.internal.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:152)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:231)
at org.hibernate.event.internal.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:102)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:55)
at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)
at com.dc.core.security.container.SecureSessionProxy.flush(SecureSessionProxy.java:306)
at com.dc.core.operations.service.impl.OperationService.performSingleOperation(OperationService.java:778)
at com.dc.core.presentation.presenter.impl.ScreenPresenter.performSingleOperation(ScreenPresenter.java:1187)
at com.dc.core.presentation.controller.springmvc.impl.ItemScreenRenderController.performOperation(ItemScreenRenderController.java:952)
at com.dc.core.presentation.controller.springmvc.impl.ItemScreenRenderController.performItemScreenOperation(ItemScreenRenderController.java:229)
at com.dc.core.presentation.controller.springmvc.impl.ItemScreenRenderController.performOperation(ItemScreenRenderController.java:433)
at sun.reflect.GeneratedMethodAccessor1397.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at com.dc.core.common.controller.impl.CustomMultiActionController.invokeNamedMethod(CustomMultiActionController.java:504)
at com.dc.core.common.controller.impl.CustomMultiActionController.handleRequestInternal(CustomMultiActionController.java:430)
at com.dc.core.common.controller.impl.BaseMultiActionController.handleRequestInternal(BaseMultiActionController.java:623)
at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:154)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:50)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:938)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:870)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:646)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)

如果我捕获 Exception 而不是 ConstraintViolationException,则会看到以下错误

  Caused by: org.hibernate.exception.ConstraintViolationException: could not execute statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:129)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:96)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:58)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3032)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3558)
at org.hibernate.action.internal.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:98)
at org.hibernate.engine.spi.ActionQueue.execute(ActionQueue.java:490)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:195)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:179)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:214)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:324)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
at org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
at org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
at org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
at org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
at com.dc.core.security.container.SecureSessionProxy.save(SecureSessionProxy.java:685)
at com.dc.core.common.dao.impl.GenericWriteDAO.create(GenericWriteDAO.java:37)
at com.dc.core.entity.dao.impl.EntityInstanceDAO.create(EntityInstanceDAO.java:63)
at com.dc.core.entity.service.impl.EntityService.create(EntityService.java:278)
at com.dc.core.api.EntityAPI.saveInstance(EntityAPI.java:188)

Caused by: com.microsoft.sqlserver.jdbc.SQLServerException: Violation of UNIQUE KEY constraint 'UQ__P_USER_S__B9BE370E29ACF837'. Cannot insert duplicate key in object 'dbo.P_USER_SECURITY_QUESTIONS'. The duplicate key value is (9).
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:216)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1515)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:404)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:350)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:5696)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1715)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:180)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:155)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.executeUpdate(SQLServerPreparedStatement.java:314)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208)
... 162 more

有人可以帮我吗,有没有更好的方法在 catch block 中执行更新操作?

最佳答案

你可以试试这个:将两个更新保留在不同的事务中。如果try中出现异常,则回滚catch中的第一个Transaction,清除 session 并启动新的Transaction在catch中更新。

    Transaction tx = null;
try {
tx = session.beginTransaction();
services.entity.saveInstance(secQuestions);
tx.commit();
}
catch(Exception e) {
tx.rollback();
session.clear();
if(e instanceof org.hibernate.exception.ConstraintViolationException){
Transaction tx1 = session.beginTransaction();
userRegistrationRequest.requestProcessed = true
infoMsg = userRegistrationRequest.infoMessage+' Exception: '+e.message
userRegistrationRequest.infoMessage = infoMsg
entityService.update(userRegistrationRequest);
tx1.commit();
logger.info("Caught BaseApplicationException "+e.message)
return;
}
}

关于java - hibernate 断言失败: null id in entry (don't flush the Session after an exception occurs),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25896378/

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