作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我创建了两个简单的Grails V3域类,其中位置是这样嵌入父级场所中的属性类型的
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
}
}
@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
}
最佳答案
如果我正确理解cascade: "all-delete-orphan"
仅当您有hasMany=[something:Something]
时才需要
在您的情况下,如果我要创建这样的关系,则hasOne
或GeoAddress location
可能是更好的设置。我知道两者之间会有细微的差异。
无论如何,您都在进行理论上的测试。我认为您需要捕获错误才能开始弄清楚为什么它没有将预期的行为串联起来。所以要么
if (!v.delete(flush:true) {
println "--- ${v.errors}"
}
try catch block
lookupVenue .removeFromLocation(loc)
关于grails - 无法获得级联保存,无法在嵌入式类引用上删除以正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42054344/
我是一名优秀的程序员,十分优秀!