0
votes

I'm working in grails 2.3.8

We have the following domain classes:

abstract class GroupItem {
  static belongsTo = [group:AppGroup]
  static mapping = {
    tablePerHierarchy = false
  }
}

class MyDomain extends GroupItem {
  String name
  String type
  int Identifier

  static hasMany = [attributes:Attribute]

  static mapping = {
    attributes cascade: 'all-delete-orphan'
  }
}

class Attribute {
  String name
  String value
  static belongsTo = [myDomain:MyDomain]
}

In my controller's 'update' method I am trying to update a 'MyDomain' object. I can update the attributes that associate with the domain fine, however when I try and change any of the properties on an instance itself (name, identifier or type) the changes don't persist. The controller code:

def update(Long id) {
  --- 
  def myDomain = MyDomain.get(id)
  myDomain.type = 'DIFFERENT TYPE'
  myDomain.save(flush:true, failOnError:true)
}

However the signal in the database still has the same type as when it was created. To try and follow the logic I added a beforeUpdate method to the domain class that prints out the type and I can see that it is getting called and in beforeUpdate it thinks the type is my new value, however the update is never persisted. There are no exceptions, the objects validate, and nothing in any logs. Any clue what could be causing this?

Cheers!

1
Some errors? if no, try beforeInsert() instead of beforeUpdate()Koloritnij
Nope no errors. Just tried checking and it seems to believe the object isn't dirty.DJOodle

1 Answers

0
votes

Right... Slaps Face

The bits that I didn't include in that post was that they update is actually happening within a service (with the @Transactional annotation, and Im also using a command object to do some validation earlier in the controller. The Command object checks the group id is valid by seeing if it a non-null is returned by

def group = AppGroup.read(id).

I'm not sure why but it seems that since this object is in the session this is the one that's getting saved at the end of the transaction and it isn't dirty. If I discard this group once I am done with it, then everything works fine.

Not sure why this is, but hey ho, fixed my problem. Might try to see if exists() is a better fit...