无法获得级联保存或在嵌入式类ref上删除也无法工作

问题描述 投票:0回答:4

我创建了两个简单的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
    }
}

但是,当我编写一个集成测试时,我发现位置的级联创建不起作用(在传递到场地之前,我必须保存该位置,不再是临时的。

[另外,当我在启用了flush:true的场所运行删除操作,并查询地址时,我仍然获得返回的嵌入式地址-我认为使用flush:true会看到我的GeoAddress级联删除,但是我的测试失败因为我按预期使用GeoAddress.get(loc.id)时没有得到null]

@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)条目?

我创建了两个简单的Grails V3域类,其中位置是在父场所中嵌入属性类型的,例如import java.time.LocalDate class Venue {String name LocalDate ...

grails gorm hibernate-cascade
4个回答
0
投票

如果我理解正确的话


0
投票

很奇怪,太累了,现在不知道了。我尝试了外部实体和嵌入式实体-请参阅下面的调整模型。


0
投票

我认为这是一个错误的配置。嵌入式意味着实体被嵌入域类内部。通常这是一个普通的POJO,位于domainclass文件夹(和src / groovy文件夹)之外。嵌入实体的所有字段都包含在嵌入实体的表中。 hasone设置两个域类实体之间的关系。因此,要么使用嵌入式,要么使用hasOne,但不要同时使用两者。


0
投票

确定-我仔细阅读并寻找了差异-如果我在将嵌入式GeoAddress传递给像这样的场地构造函数之前进行了保存(修改后的简单测试),就会发生这种情况

© www.soinside.com 2019 - 2024. All rights reserved.