hibernate - 如何创建 Grails 域对象但不保存?

标签 hibernate grails grails-orm

我有一个应该创建域对象的方法。但是,如果在构建对象的过程中发生了某种情况,该方法应该只返回而不保存对象。

给定:

class SomeDomainObjectClass {
    String name
}

class DomCreatorService {
    def createDom() {
        SomeDomainObjectClass obj = new SomeDomainObjectClass(name: "name")

        // do some processing here and realise we don't want to save the object to the database
        if (!dontWannaSave) {
            obj.save(flush: true)
        }
    }
}

在我的测试中(服务是 DomCreatorService 的实例):

expect: "it doesn't exist at first" 
SomeDomainObjectClass.findByName("name") == null

when: "we attempt to create the object under conditions that mean it shouldn't be saved"
// assume that my test conditions will mean dontWannaSave == true and we shouldn't save     
service.createDom() 

then: "we shouldn't be able to find it and it shouldn't exist in the database"
// check that we still haven't created an instance
SomeDomainObjectClass.findByName("name") == null

我的最后一行失败了。为什么最后一个 findByName 返回 true,即为什么可以找到我的对象?我以为它只会找到保存到数据库中的对象。我应该测试什么以查看我的对象是否尚未创建?

最佳答案

您一定遗漏了一些东西——如果您只是创建一个实例但不对其调用任何 GORM 方法,Hibernate 就无法知道它。它开始管理对象的唯一方法是通过初始 save() 调用“附加”它。没有它,它就像任何其他对象一样。

您将看到的是@agusluc 所描述的,但在这里不会影响您。如果您加载以前持久化的实例并修改任何属性,在请求结束时,Hibernate 的脏检查将启动,检测到这个附加但未保存的实例是脏的,它会将更改推送到数据库,即使您不调用 save()。在这种情况下,如果您不希望编辑持续存在,请通过调用 discard() 从 session 中分离对象。这与您所看到的无关,因为您没有附加任何东西。

可能已经有一个具有该 name 属性的实例。检查 findByName 查询返回的其他属性,我假设您会看到它是以前持久化的实例。

此外,这是单元测试还是集成?请务必使用集成测试来实现持久性 - 单元测试模拟不适用于测试域类,它只应该用于在测试其他类型的类(例如 Controller )时为域类提供 GORM 行为。

关于hibernate - 如何创建 Grails 域对象但不保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27570653/

相关文章:

hibernate - Grails 中的缓存 - 默认情况下缓存什么以及可以启用什么?

java - 如果数据库表列设置为 auto_increment 那么我们为什么要使用 @ generatedValue

java - Hibernate中save()和update()有什么区别

java - UTF-8 不会在 Hibernate + MySQL 上持续存在

java - 为什么我重装win7后无法安装grails插件?

java - 如何将 jar 中的域类导入到 Micronaut 项目中?

hibernate - WildFly 9.0.1.Final 附带了哪些版本的库

Grails 2.0 : No module found with name [window]

sql - Grails查询以过滤关联并仅返回匹配的实体

Grails GORM 继承改变判别器列