Grails:是否有可能阻止域类实例被持久化? [英] Grails: Is it possible to prevent a domain class instance from being persisted?

查看:82
本文介绍了Grails:是否有可能阻止域类实例被持久化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要创建一个Domain类的(一个)实例(如预期的那样,它具有到我的数据库的GORM接口),并仅将其用作容器来传递数据,就像一个Map对象。我想确保 永远永远不会持久保存在数据库中。恐怕GORM和它的聪明之处,即使没有明确地调用 save(),也会以某种方式将它保存在场景后面的数据库中。当实例化我的对象时,是否有办法指定不要持久这个子句?我知道如何防止在一个域类上的持久化,我想要的只是防止在一个特定的类的实例上的持久性。

I want to create an (one) instance of a Domain class (which, as expected, has a GORM interface to my database) and only use it as a container to pass data around, like a Map object. I want to make absolutely sure that my instance is never going to get persisted in the database. I'm afraid that GORM, with all its cleverness, will somehow manage to save it in the database behind the scene even without an explicit call to save(). Is there a way to specify a "do not persist this" clause when instantiating my object? I know how to prevent persistence on a domain class, what I want is to prevent persistence on a particular instance of the class only.

我现在的解决方案是在> groovy / src / 中创建一个具有相同属性和方法的类,并将其用作我的数据容器,并根据需要进行类型转换。它感觉错了,DRY失败了,而且还没有成功。

The solution I have now is to create a class in groovy/src/ that carries the same properties and methods, and use it as my data container, and do type casts as required. It feels wrong, fails DRY, and hacky.

当然你也可以告诉我,我应该停止这么偏执狂,Grails永远不会坚持一个领域类实例没有明确的保存。

Of course you may also tell me that I should stop being so paranoid and that Grails is never going to persist an domain class instance without an explicit save.

推荐答案

显式调用实例上的 save()。你可以创建一个实例并传递它,它不会被持久化。

Grails will not save an instance of your domain class without an explicit call to save() on the instance. You can create an instance and pass it around, and it will not be persisted.

这篇关于Grails:是否有可能阻止域类实例被持久化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆