0
   def names = domain.dirtyPropertyNames
    for (name in names) {
        def originalValue = domain.getPersistentValue(name)
        def newValue = domain."$name"
    }

But if i have a relation of 1-1 with other domain

how can i access dirtyPropertyNames for that other domain

def dirtyProperties = domain?.otherDomain?.dirtyPropertyNames
    for (name in dirtyProperties ) {
        def originalValue = domain?.otherDomain?.getPersistentValue(name)
        def newValue = domain?.otherDomain?."$name"
    }

But i am getting No such property: dirtyPropertyNames for class: otherDomain

Apu
  • 21
  • 3

1 Answers1

1

This seems not to be an issue when tested against Grails 2.2.4 and 2.3.0.
How have you tailored the 1:1 relationship?

Here is a sample, hope that helps:

class Book {
    String name
    String isbn
    static hasOne = [author: Author]
}

class Author {
    String name
    String email
    Book book
}

//Save new data
def book = new Book(name: 'Programming Grails', isbn: '123')
book.author = new Author(name: "Burt", email: 'test', book: book)
book.save(flush: true)
//Sanity check
println Book.all
println Author.all

//Check dirty properties of association
def book = Book.get(1)
book.author.name = 'Graeme'

def dirtyProperties = book?.author?.dirtyPropertyNames
for (name in dirtyProperties ) {
    println book?.author?.getPersistentValue(name) //Burt
    println book?.author?."$name" //Graeme
}

Although, wrt Grails 2.3.0 you can persist a 1-1 relation as done below unlike above:

def author = new Author(name: "Burt", email: 'test')
def book = new Book(author: author, name: 'PG', isbn: '123').save(flush: true)
dmahapatro
  • 49,365
  • 7
  • 88
  • 117