问题
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.
I have read that grails 3 release has some enhancements to dirty checking & this could be a bug or I am missing something.
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().
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.
Note: I have done similar stuff in Grails 2.4 & it used to work.
Domain class is as follows:
package com.perseus
class Derived extends Base{
static constraints = {
name blank: false, nullable: false
}
}
Base class in src/main/groovy:
package com.perseus
class Base implements Serializable {
private static final long serialVersionUID = 1L
String name
}
Controller
package com.perseus
class DerivedController {
static scaffold = Derived
}
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.
How to reproduce:
- Run the app.
- Create a new model object (model name is Derived)
- Modify the object using edit view & click Update.
- You will see that modifications are not persisted.
回答1:
Finally I found a reference here which explains the reason for failure to update dirty check status.
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:
- Business model classes
- Business logic classes
- User Interface
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'
Design wise we like our model classes in a project completely independent of UI or persistent technologies like hibernate. So that these model classes can be used in variety of different projects without any additional dependencies. And we achieved this with grails so far by creating a new class that extends actual model class in our library
Is there any way I can solve this problem, without modifying the base class?
In short, model classes (POJO) are now dependent on grails framework, whereas in earlier versions they were not.
来源:https://stackoverflow.com/questions/47870593/grails-3-1-1-dirty-checking-not-working-when-model-class-extends-another-groov