- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
考虑以下代码:
class User {
static constraints = {
email email: true, unique: true
token nullable: true
}
String email
String password
String token
}
<小时/>
@TestFor(User)
@TestMixin(DomainClassUnitTestMixin)
class UserSpec extends Specification {
def "email should be unique"() {
when: "twice same email"
def user = new User(email: "test@test.com", password: "test")
def user2 = new User(email: "test@test.com", password: "test")
then: "saving should fail"
user.save()
!user2.save(failOnError: false)
}
}
使用以下配置(部分),application.yml:
grails:
gorm:
failOnError: true
autoFlush: true
为什么user2.save(failOnError: false)
由于没有保存到数据库而没有返回false
?
运行输出:grails test-app *UserSpec
:
businesssoftware.UserSpec > email should be unique FAILED org.spockframework.runtime.ConditionNotSatisfiedError at UserSpec.groovy:40
当我用 user.save(flush: true)
替换 user.save()
时,它确实工作。
但是 https://grails.github.io/grails-doc/latest/guide/conf.html 的文档,第 4.1.3 节声称:
grails.gorm.autoFlush - If set to true, causes the merge, save and delete methods to flush the session, replacing the need to explicitly flush using save(flush: true).
作为引用,这是 grails --version
的输出:
| Grails Version: 3.0.2
| Groovy Version: 2.4.3
| JVM Version: 1.8.0_40
这正是我正在做的事情,我在这里错过了什么?
最佳答案
我认为 autoFlush 文档具有误导性。
grails.gorm.autoFlush - If set to true, causes the merge, save and delete methods to flush the session, replacing the need to explicitly flush using save(flush: true).
看一下 GormInstanceApi 的 save() 方法和 doSave() 。在 doSave() 结束时,您将看到仅当刷新参数为 true 时才会刷新 session :
if (params?.flush) {
session.flush()
}
代码中没有任何内容表明刷新参数可以来自传递给 save() 的参数之外的任何其他位置。
我找不到代码来证明这一点,但我认为 autoFlush 在 Hibernate session 关闭时发挥作用,例如当 Controller 或服务方法存在/返回时。
此外,用于 Spock 和 JUnit 的 GORM 实现并不是真正的 GORM,因此它甚至可能不使用 autoFlush 配置。
我要在这里冒险了。我还没有尝试过这个,但是您也许能够手动刷新 session 。
@TestFor(User)
@TestMixin(DomainClassUnitTestMixin)
class UserSpec extends Specification {
def "email should be unique"() {
when: "twice same email"
def user = new User(email: "test@test.com", password: "test")
def user2 = new User(email: "test@test.com", password: "test")
simpleDatastore.currentSession.flush()
then: "saving should fail"
user.save()
!user2.save(failOnError: false)
}
}
simpleDataStore 来自DomainClassUnitTestMixin .
关于grails - 为什么 grails.gorm.autoFlush 设置为 true 不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31049594/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!