我试图在一个catch块中执行一些更新操作。冬眠例外ConstraintViolationException抛出如下
我正在使用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块中执行更新时,我遇到以下错误
[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)
如果我抓住异常而不是约束违反异常看到下面的错误
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
有人能帮帮我吗,有没有更好的方法来执行捕获块中的更新操作?
您可以尝试这样做:在不同的事务中保留这两个更新。如果try中出现异常,请回滚catch中的第一个事务,清除会话并启动一个新事务以在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;
}
}
我有一个hibernate和JSF2应用程序进入部署服务器,突然抛出一个org。冬眠AssertionFailure:异常中的空id。我将立即提供堆栈跟踪和代码,但首先有四个重要问题: > 这只发生在部署服务器(Jboss)上 研究这一点,人们在尝试插入对象时似乎会出现这种错误。但是当我做一个简单的查询时,我得到了错误。(实际上,各种不同的查询,因为错误会随机出现在多个页面上。) 错误只会偶尔出现
你好,我是PHP中的web开发人员,最近迁移到javaEE。我在mysql中创建表。这是第一类: . . . 这是我的第二节课: . . .
编辑:得到一个-1,你能解释一下为什么吗?我搜索了重复项,但没有找到任何重复项。 为我刚刚遇到的问题发布Q/a: 例外情况: org.hibernate.AssertionFailure: null id in xyz (在发生异常后不要刷新会话)
我设置了一个非常基本的Grails 3 web应用程序,使用jdbc连接到PostgreSQL数据库。下面您可以找到< code>Cluster域类和专用服务的代码。 碰巧用同一个< code>slug参数调用了< code>createCluster方法两次: 导致以下异常 我以为违反一约束会被方法拦截,但显然我错了。好吧,所以我添加了一个try/catch,但它基本上没有效果,异常没有被包装,
我需要从遗留系统进行数据迁移,总共有20,000条记录[所有数据将由csv文件提供],由于一些技术原因,我必须使用JPA/Hibernate将这些数据导入到您当前使用的新系统中。 当我进行导入时,总是遇到如下交易问题: 数据库是 MS SQL Server 2005 org . hibernate . assertion failure:< code > xxxx 条目中的id为空(发生异常后不要
问题内容: 当取消注释时,我们将在第二种方法中获得en异常- 当作者。 为什么会这样呢? 问题答案: 问题似乎是Hibernate引发了一个异常(因此当前事务无效),但是随后您尝试在该会话中继续执行更多操作。 正确的方法是将您使用的测试分为2部分,一部分测试空作者,另一部分测试有效作者。 在生产代码(例如控制器)上,您需要重新启动操作(关闭事务,重新启动流程)才能继续。但是,考虑到游戏管理交易的方