简体   繁体   English

与GORM建立关系

[英]grails GORM relationship

Hi I don't really understand whay this doesnt work, but im 99% sure its a GORM issue. 嗨,我不太了解这行不通,但是我99%的人肯定这是GORM问题。

here's my domains: 这是我的网域:

 Class Product{
    String name
    static hasMany = [parts:Parts]
    static mappedBy = [parts:'product']
 }


 Class Parts{
    Product product
    static hasMany = [alternatives:Alternatives] 
 }

so a Product is made up of parts and a part points to a product When I do the following it doesn't save to the database. 因此,产品由零件组成,一部分指向产品。当我执行以下操作时,它不会保存到数据库中。

 Product p = new Product(name:"test")
 Product part1 = Product.get(2)
 Product part2 = Product.get(3)
 Parts c =  new Parts(product: part1).save(flush:true,failOnError:true)
 Parts c2 =  new Parts(product: part2).save(flush:true,failOnError:true)

 p.addToParts(c)
 p.addToParts(c2)

 p.save(flush:true,failOnError:true)

This doesn't throw any error but also doesn't persist anything to the database. 这不会引发任何错误,但也不会对数据库持久化任何内容。

Any Ideas? 有任何想法吗?

Product p = new Product(name: "test")
Product part1 = Product.get(2)
Product part2 = Product.get(3)

p.addToParts(new Parts(product: part1))
p.addToParts(new Parts(product: part2))
p.save(flush: true, failOnError: true)

The best way to do this is to create the new Parts within your .addToParts() call. 最好的方法是在.addToParts()调用中创建新的部件。

It also seems as though you should have static belongsTo = Product within your Parts class. 似乎您的Parts类中也应该具有static belongsTo = Product (Only allowing one side of the Many-to-Many relationship to persist can cause less confusion) (仅允许多对多关系的一侧保持不变可以减少混乱)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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