我创建了两个简单的 Grails V3 域类,其中位置是父 Venue 中的嵌入属性类型,如下所示
import java.time.LocalDate
class Venue {
String name
LocalDate dateCreated
LocalDate lastVisited
LocalDate lastUpdated
GeoAddress location
static hasOne = [location:GeoAddress]
static embedded =['location']
static constraints = {
lastVisited nullable:true
location nullable:true
}
static mapping = {
location cascade: "all-delete-orphan", lazy:false //eager fetch strategy
}
}
class GeoAddress {
String addressLine1
String addressLine2
String addressLine3
String town
String county
String country = "UK"
String postcode
static belongsTo = Venue
static constraints = {
addressLine1 nullable:true
addressLine2 nullable:true
addressLine3 nullable:true
town nullable:true
county nullable:true
country nullable:true
postcode nullable:true
}
}
但是,当我编写集成测试时 - 我发现位置的级联创建不起作用(我必须在传递到场地之前保存它不再是瞬态的位置。
此外,当我在启用了 flush:true 的场地上运行删除并查询地址时,我仍然得到返回的嵌入式地址 - 我认为使用 flush:true 我会看到我的 GeoAddress 级联删除,但我的测试失败了如我所料,使用 GeoAddress.get(loc.id) 时不要得到空值
@Integration
@Rollback
class VenueIntegrationSpec extends Specification {
void "test venue with an address" () {
when: "create a venue and an address using transitive save on embedded "
GeoAddress address = new GeoAddress (addressLine1: "myhouse", town: "Ipswich", county: "suffolk", postcode : "IP4 2TH")
address.save() //have to save first - else Venue save fails
Venue v = new Venue (name: "bistro", location: address)
def result = v.save()
then: "retrieve venue and check its location loaded eagerly "
Venue lookupVenue = Venue.get(v.id)
GeoAddress loc = lookupVenue.location
loc.postcode == "IP4 2TH"
loc.town == "Ipswich"
when: " we delete the venue, it deletes the embedded location (Address)"
v.delete (flush:true)
GeoAddress lookupLoc = GeoAddress.get (loc.id)
then: "address should disppear"
lookupLoc == null
}
我以为我已经正确设置了这个,但显然我没有。为什么我对 Venue.save() 和 delete() 的级联操作不会级联到我的嵌入式位置 (GeoAddress) 条目?