我在实体之间有一对多的关系 - RentalUnit 和 Review 。所有测试都运行良好,除非我向不同的 RentalUnit 实例添加相同的评论,如下所示:
def review3 = Review.build().save(flush: true)
def rentalUnit2 = RentalUnit.build().
addToReviews(review2).addToReviews(review3).save(flush: true)
assert rentalUnit.reviews.contains(review2)
似乎 GORM 和该addTo*
方法似乎并不关心我是否添加了相同的 Review 实例,所以我猜我的域类中缺少一些东西。那会是什么?
谢谢你的帮忙
ps
class Review {
String submittedBy
String content
String dateReceived
boolean isApproved
static belongsTo = RentalUnit
static mapping = {
content type: 'text'
}
static constraints = {
submittedBy blank: false, size: 3..50
content blank: false, size: 5..2500
}
}
class RentalUnit {
String name
String nickname
Address address
static hasMany = [reviews:Review]
static mapping = {
reviews cascade: "all-delete-orphan"
}
static constraints = {
name blank: false, unique: true, size: 4..10
nickname blank: false, size: 5..60
}
}