0

I have an existing application with the following domain classes/relations:

class Contact {
 static hasMany = [phones: Phone]
}

I would like to modify the structure to allow for a delete flag to be set in the Phone class and then not have to modify existing calls to the getter for phones to get phones that have not had that delete flag set.

ex:

contact.phones

I have seen a post on this site detailing how to do this. StackOverflowPost

I have tried that approach(modifications showed below) but GORM is behaving oddly by incrementing the version when it shouldn't be. I'll hit the contact edit page and then when a version comparison is done on the server after submitting the form the version in the DB has incremented up about 5 or 6 times compared to the version value that was passed to the edit.gsp. I have also applied this pattern to two more relations so I'm thinking they are also causing the version to increment. I'm going to test with only one domain relation modification to see if I can find a pattern. Does anyone have any ideas as to the right way to do this or how to avoid version incrementing?

class Contact {

 Set<Phone> phones

 static hasMany = [phones: Phone]

 Set<Phone> getPhones() {
    phones.findAll { !it.deleted }
 }

}
JSchins
  • 117
  • 10
  • If you won't use cascade operations you can remove hasMany and phones, and your getter will be looks like Phone.findAllByContactAndIsDeleted(this, false) – Evgeny Smirnov Aug 05 '17 at 17:54

1 Answers1

1

Not a direct answer to your question, but i use methods in my class, which instead of returning the actual domain instances, returns the detached criteria that callers can use, add new restrictions, and finally get the results.

​class Contact {

  DetachedCriteria<Phone> phones() {
   def criteria = new DetachedCriteria(Phone).build {
     eq "deleted", false
     eq "contact", this
   }

   return criteria
 }
}​

The contact class will return a criteria with main restrictions added, and callers can add more restrictions if they have to.

List phones = contact.phones().list()
or 
List phones = contact.phones().list {
  eq "country", "xxx"
}

This gives callers more flexibility.

Sudhir N
  • 4,008
  • 1
  • 22
  • 32