- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下代码将新对象添加到数据库中。首先,它从数据库中获取另一个对象并添加到最终对象中。
我的代码几行
ClassC c = ClassC.findByName(cName)
ClassD d = new ClassD(
name: "WHATEVER",
classC: c
)
print "AAA\n"
ClassC.withTransaction {
c = c.merge()
// c.save(failOnError: true, flush: true)
}
print "BBB\n"
// ClassD.withTransaction {
// d = d.merge()
// }
// print "CCC\n"
ClassD.withTransaction {
d.save(failOnError: true, flush: true)
}
print "DDD\n"
AAA
BBB
2013-07-31 13:57:14,279 ERROR JobRunShell - Job DEFAULT.1 threw an unhandled Exception:
org.springframework.dao.DuplicateKeyException: a different object with the same identifier value was already associated with the session: [xxx.ClassD#15]; nested exception is org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [xxx.ClassD#15]
ClassC.withTransaction {
ClassC c = ClassC.findByName(cName)
// find the record with name: "WHATEVER" or create a new one if there is none
ClassD d = ClassD.findOrCreateWhere(name: "WHATEVER")
c = c.merge()
c.addToClassesD(d) // static hasMany = [classesD: ClassD] <-- in ClassC domain
c.save(failOnError: true, flush: true)
}
c.addToClassesD(d)
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) at org.springframework.web.context.request.RequestContextHolder$currentRequestAttributes.call(Unknown Source) at xxx.AuditLogService.getCurrentUser(AuditLogService.groovy:127) at xxx.AuditLogService$getCurrentUser.callStatic(Unknown Source) at xxx.AuditLogService$_closure2.doCall(AuditLogService.groovy:58) 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.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90) 2013-08-02 09:39:11,110 ERROR ErrorLogger - Job (DEFAULT.1 threw an exception. org.quartz.SchedulerException: Job threw an unhandled exception. at org.quartz.core.JobRunShell.run(JobRunShell.java:224) at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:557) Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request. at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131) at org.springframework.web.context.request.RequestContextHolder$currentRequestAttributes.call(Unknown Source) at xxx.AuditLogService.getCurrentUser(AuditLogService.groovy:127) at xxx.AuditLogService$getCurrentUser.callStatic(Unknown Source) at xxx.AuditLogService$_closure2.doCall(AuditLogService.groovy:58) 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.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243) at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:90)
最佳答案
您可能只应在一个事务中完成所有操作。
至于您遇到的错误,我假设您已经为名称设置了unique
-当已经有名称为“WHATEVER”的记录时,您将收到该错误。
您可能想要执行以下操作:
ClassC.withTransaction {
ClassC c = ClassC.findByName(cName)
// find the record with name: "WHATEVER" or create a new one if there is none
ClassD d = ClassD.findOrCreateWhere(name: "WHATEVER")
c = c.merge()
d.classC = c
d.save(failOnError: true, flush: true)
}
关于hibernate - Grails-DuplicateKeyException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17969795/
我有以下代码将新对象添加到数据库中。首先,它从数据库中获取另一个对象并添加到最终对象中。 我的代码几行 ClassC c = ClassC.findByName(cName)
我正在研究 db2。我有一张表,其中包含一些数据: Data | Value | Lang 对该表的数据列添加了唯一约束。但是,现在应用程序可以包含同一日期的许多行。因此,我删除了约束,但是,在重组表
我有两个 SQL 表,每个表都有一个由三个不同的整数字段组成的复合键。我添加了如下代码来向 Table1 添加一条记录。 try { Table1 newRow = new Table1
我正在使用 JooQ 在 Aurora MySQL 上执行 CRUD 操作。 我有以下情况: 如果由于重复键异常导致插入失败,则再次重试相同的操作,否则忽略该异常。 如果插入失败,则在满足条件的情况下
我有两个对象:User 和 Resume。 Resume 有一个 User user 字段。在我的代码中,我首先得到一个 user 对象并对其进行一些更改。如果 Resume.findByUser(u
当我尝试使用方法 [1] 更新插入(增量或插入)文档时,我确实会不时收到此错误 [2]。 [1] public NGram save(final NGram ngram) { Criteria
简而言之:与许多教程不同,对象“logger”无法被识别。 问题本身并不是很严重,我可以轻松解决它。然而,看到这个“记录器”在我的 intellIj 编辑器中保持红色状态是非常令人沮丧的。我正在浏览文
下面列出的代码尝试更新数据库中的一行,但抛出异常: System.Data.Linq.DuplicateKeyException: Cannot add an entity with a key th
我在处理 DataAccessException 时遇到了麻烦。 当违反唯一键约束时,我得到了 JpaSystemException,而不是 DuplicateKeyException! 我发现一些线
下面是我在线程 Spring 执行器下的类(class)。根据源/服务的类型 TAXP、TAXS、TAXT 方法被调用。 逻辑是如果“taxInfo.getGroupingId()”已经存在于主税表中
我有大量的数据希望使用GORM加载到数据库中。 class DbLoadingService { static transactional = false // these ar
我正在为一些关键业务操作编写审计服务。该服务正在使用 IoC 模式实现: public interface IAuditWriter { void WriteAction(int key, s
是否有类似 DuplicateKeyException 的东西适用于 javax.persistence.*? 我发现这个异常仅适用于 ejb 库:DuplicateKeyException 谢谢。
我在使用 Oracle 数据库的 WebLogic 上部署了一个相对简单的 Web 应用程序。当我完成应用程序的开发并准备将其部署到生产 WebLogic 实例所在的实际 Linux 服务器时,应用程
我正在使用 Spring Boot 和 spring data MongoDB(+mongodb-reactive,但我想这对于这个问题并不重要)。对于一些自动生成,我使用 Lombok。 我有一个U
插入数据的方法 public String home() { try { MongoData data=new MongoData(); data.setD
我正在使用 SpringBoot 和 jdbc 模板,并在获取一些重复数据时执行批量插入。我想忽略重复的行,系统应该继续插入,但我收到异常并且插入被回滚。我怎样才能继续插入数据并保留重复的行。 使用下
我有一个带有 @Document(collection = "excursionAttendees") 注释的 POJO ( ExcursionAttendee ) 注释,并且有一个没有任何自定义方法
正如标题中提到的,当同一个客户端同时查询 token 端点时(两个进程同时为同一个客户端请求 token ),我遇到了这个问题。 身份验证服务器日志中的消息如下所示: 2016-12-05 19:08
我正在使用 Spring-Data/MongoDB 并在保存/插入时正确捕获重复键。 例如,假设我有一个用户被保存到一个集合中。 User 对象用两个 @Indexed(unique=true) (两
我是一名优秀的程序员,十分优秀!