Skip to content

Commit 2f27f48

Browse files
committed
#1294 Update DirtyCheckable.groovy
* Cleanup and remove redundant code. * Update implementation of markDirty to consider EntityProxy and update the comparison operation accordingly.
1 parent 0f6c78c commit 2f27f48

File tree

2 files changed

+55
-19
lines changed

2 files changed

+55
-19
lines changed

grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/dirty/checking/DirtyCheckable.groovy

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ trait DirtyCheckable {
7878
void markDirty(String propertyName, newValue) {
7979
if( $changedProperties != null && !$changedProperties.containsKey(propertyName)) {
8080
def oldValue = ((GroovyObject) this).getProperty(propertyName)
81-
boolean isNull = newValue == null
82-
if ((isNull && oldValue != null) ||
83-
(!isNull && oldValue == null) ||
84-
(!isNull && !newValue.equals(oldValue))) {
85-
$changedProperties.put propertyName, oldValue
86-
}
81+
markDirty(propertyName, newValue, oldValue)
8782
}
8883
}
8984

@@ -97,12 +92,17 @@ trait DirtyCheckable {
9792
boolean isNull = newValue == null
9893
if ((isNull && oldValue != null) ||
9994
(!isNull && oldValue == null) ||
100-
(!isNull && !newValue.equals(oldValue))) {
95+
(!isNull && (newOldOrOldValueIsProxy(newValue, oldValue) && newValue.getAt("id") != oldValue.getAt("id")
96+
|| (!newOldOrOldValueIsProxy(newValue, oldValue) && !newValue.equals(oldValue))))) {
10197
$changedProperties.put propertyName, oldValue
10298
}
10399
}
104100
}
105101

102+
boolean newOldOrOldValueIsProxy(newValue, oldValue) {
103+
(newValue instanceof EntityProxy || oldValue instanceof EntityProxy)
104+
}
105+
106106
/**
107107
* @return A list of the dirty property names
108108
*/
@@ -132,4 +132,4 @@ trait DirtyCheckable {
132132
return null
133133
}
134134
}
135-
}
135+
}

grails-datastore-gorm-test/src/test/groovy/org/grails/datastore/gorm/DirtyCheckingSpec.groovy

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -110,26 +110,62 @@ class DirtyCheckingSpec extends GormDatastoreSpec {
110110
TestBook.deleteAll()
111111
TestAuthor.deleteAll()
112112
}
113+
114+
void "test relationships are marked dirty when proxies are used but different"() {
115+
given:
116+
Long bookId = new TestBook(title: 'Martin Fierro', author: new TestAuthor(name: 'Jose Hernandez'))
117+
.save(flush: true, failOnError: true)
118+
.id
119+
Long otherAuthorId = new TestAuthor(name: "JD").save(flush: true, failOnError: true).id
120+
session.flush()
121+
session.clear()
122+
123+
when:
124+
TestBook book = TestBook.get(bookId)
125+
book.author = TestAuthor.load(otherAuthorId)
126+
127+
then:
128+
proxyHandler.isProxy(book.author)
129+
book.isDirty('author')
130+
book.isDirty()
131+
132+
cleanup:
133+
TestBook.deleteAll()
134+
TestAuthor.deleteAll()
135+
}
136+
137+
void "test relationships marked dirty when domain objects are used and changed"() {
138+
139+
given:
140+
Long bookId = new TestBook(title: 'Martin Fierro', author: new TestAuthor(name: 'Jose Hernandez'))
141+
.save(flush: true, failOnError: true)
142+
.id
143+
Long otherAuthorId = new TestAuthor(name: "JD").save(flush: true, failOnError: true).id
144+
session.flush()
145+
session.clear()
146+
147+
when:
148+
TestBook book = TestBook.get(bookId)
149+
book.author = TestAuthor.get(otherAuthorId)
150+
151+
then:
152+
!proxyHandler.isProxy(book.author)
153+
book.isDirty('author')
154+
book.isDirty()
155+
156+
cleanup:
157+
TestBook.deleteAll()
158+
TestAuthor.deleteAll()
159+
}
113160
}
114161

115162
@Entity
116163
class TestAuthor {
117164
String name
118-
long version
119-
120-
@Override
121-
boolean equals(o) {
122-
if (!(o instanceof TestAuthor)) return false
123-
if (this.is(o)) return true
124-
TestAuthor that = (TestAuthor) o
125-
if (id !=null && that.id !=null) return id == that.id
126-
return false
127-
}
128165
}
129166

130167
@Entity
131168
class TestBook {
132169
String title
133-
long version
134170
TestAuthor author
135171
}

0 commit comments

Comments
 (0)