Grails 3.1.1-当模型类扩展了另一个常规类时,脏检查不起作用 [英] Grails 3.1.1 - Dirty checking not working when model class extends another groovy class
问题描述
我有一个域类,该类扩展了另一个具有相同名称但在不同库中的不同包中的常规类.
I have a domain class which extends another groovy class with same name but in different package in a different library.
问题是,当我修改域类上的实例时,它没有被标记为脏& ;;因此更改不会持续存在.
The problem is when I modify instances on the domain class, it is not marked as dirty & hence changes are not persisted.
我已阅读到Grails 3发行版对脏检查和增强功能进行了一些增强.这可能是一个错误,或者我错过了一些东西.
I have read that grails 3 release has some enhancements to dirty checking & this could be a bug or I am missing something.
新对象已正确保存,没有任何问题,我对修改的域对象以及修改后的属性&使用了 isDirty()方法都返回 false .对象已附加到会话,并通过 isAttached()确认.
New objects are saved properly without any issues, I have used isDirty() method on modified domain object as well as modified properties & both return false. Objects are attached to the session, confirmed with isAttached().
为了进行复制,我使用以下代码创建了一个测试项目&尝试从使用脚手架生成的默认grails视图更新对象,但更改仍未保留.
To reproduce, I created a test project with following code & tried updating the object from default grails views that are generated using scaffolding, but still the changes are not persisted.
注意:我在Grails 2.4和amp;它曾经可以工作.
Note: I have done similar stuff in Grails 2.4 & it used to work.
域类如下:
package com.perseus
class Derived extends Base{
static constraints = {
name blank: false, nullable: false
}
}
src/main/groovy中的基类:
package com.perseus
class Base implements Serializable {
private static final long serialVersionUID = 1L
String name
}
控制器
package com.perseus
class DerivedController {
static scaffold = Derived
}
链接到github 项目.
Link to github project.
问题:即使模型已被修改,也未标记为肮脏.当模型类扩展了另一个常规类时,就会发生这种情况.
Isssue: Model is not marked dirty, even if it has been modified. This happens when a model class extends another groovy class.
如何复制:
- 运行该应用程序.
- 创建一个新的模型对象(模型名称为Derived)
- 使用编辑视图&修改对象点击更新.
- 您将看到修改不会保留.
推荐答案
Finally I found a reference here which explains the reason for failure to update dirty check status.
我添加了 @DirtyCheck 注释,它解决了问题.
I added @DirtyCheck annotation and it solved the problem.
但是,这对我们的库的设计有负面影响.
However this has a negative impact on the design of our libraries.
我们为每个类别都有单独的项目:
We have separate projects for each category:
- 业务模型类
- 业务逻辑类
- 用户界面
这个想法是让模块彼此完全独立.因此,模型和任何UI技术都可以使用业务逻辑库.现在的问题是我必须将gorm依赖项添加到我的业务模型项目中以进行注释.
The idea is to have modules completely independent of each other. So model & business logic liraries can be used by any UI technology. Now the problem is that I had to add gorm dependency to my business model project for the annotation.
compile 'org.grails:grails-datastore-gorm:6.1.7.RELEASE'
明智的设计,我们希望模型项目完全独立于UI或诸如休眠之类的持久性技术.这样,这些模型类可以用于各种不同的项目中,而无需任何其他依赖项.到目前为止,我们通过创建一个新类来实现这一目标,该类在我们的库中扩展了实际模型类
有什么办法可以解决这个问题,而无需修改基类?
简而言之,模型类(POJO)现在依赖于grails框架,而在早期版本中则不依赖.
这篇关于Grails 3.1.1-当模型类扩展了另一个常规类时,脏检查不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!