During an archive process I am copying the details from an existing domain object to a new instance of that domain. Both domain objects have a hasMany relationship:
static hasMany = [pets:Pet]
When I have the following scenario:
def ownerOne = (logic to find owner)
def ownerTwo = new Owner
****ownerTwo.pets = ownerOne.pets****
How do I do that starred line? I’ve tired this:
Set<Pet> ownerTwoPets = new TreeSet<Pet>()
for(Pet p : ownerOne.pets) {
ownerTwoPets.add(p)
}
ownerTwo.pets = ownerTwoPets
With no luck. I can do it with String objects in a hasMany without problem. But I cannot figure it out with domain objects in a hasMany
Grails has a built in method to add to a relationship like this one. Try this: