简体   繁体   中英

grails unidirectional one-to-many

this has been bugging me,

lets say i have two model, category and product

class Category {
  static hasMany = [products : Product]
  String name
}
..
class Product {
  String name
}

now, i want to delete product, which happens to exist in many category. i came up with these lines in my product beforeDelete methods

def beforeDelete = {
  Category.list()?.each{
       it.removeFromProducts(this)
    }
}

now this may works, but the way i see it, thats a lots of query for one simple task. i know i can get the same result just with a single line of sql string ("delete from category_product where product_id = ?"). but im just curious, is there more sophisticated way to achieve this in grails? (besides executing the sql string)

You could add the following lines to the Category class

static mapping = {
    products cascade: "all-delete-orphan"
}

and call delete on Category instead. Why are you using a unidirectional relation in this case? Wouldn't it be better with a bi-directional one? In the case above you iterate over all Categories each time you delete a Product which is a resource heavy operation (depending on you many categories you have of course), the better solution would be to find the Category via a bi-directional relation like this:

class Product {
    String name
    static belongsTo = [category: Category]
}

That was you can easily get to Category from Product and delete it.

See this blog post on more hints and tips.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM