@Entity
public class Contact{
List associations
static hasMany[
associations:Contact
]
static mapping[
associations cascade:"all-delete-orphan"
]
}
and i have service for deleting associations from Contact
entity like this
Contact.withTransaction{status ->
user.contacts.collect{Contact c->
c.associations.collect{Contact association->
开发者_如何学JAVA c.associations.remove(association)
}
}
}
when trying this i get org.hibernate.HibernateException:Found two representations of same collection: Contact.associations
do i making a mistake or any other way to delete associations from Contact?
first of all you should not use collect in this case. i think the appropriate method would be each. you don't need to use a withTransaction at this point, expecting you would use a new hibernate session. try this:
def assocs = []
// store collection to avoid concurrent modification exception by during delection with in each method
assocs += user.contacts.associations
assocs.each {
// because of your cascase setting all orphans will be deleted automatically
user.contacts.removeFromAssociations(it)
}
I think there's a shorter way to do the task:
def contact = Contact.get(...) // retrieve a contact
contact.associations.clear() // remove all associations of the contact
If you are not familiar with GORM, it's better to start with this blog post.
I solved the problem. Problem was because of deleting the associations that in array. When i getting contact list it also give me associations because associations are also instance of contact. So firstly, i get the contact list with sql query by getting only contacts then i deleted the associations of contacts. Much Thanks to everyone especially to Hoàng Long :)
精彩评论