在grails / gorms文档中,它说您可以将嵌入式类与顶级父域类放在同一个域类文件中-从代码 Angular 来看,它可以工作,但仍会生成GeoAddress表,并将列嵌入源中 field 表。测试数据已在会场输入-geoAddress表保持为空。
文档暗示不应生成此嵌入式表。我可以尝试将GeoAddress移到其自己的src / groovy文件中,因此将其移出grails-app / domain文件夹,但是然后我必须“记住我已经这样做”。与包含类一起保存在同一文件中会更加“干净”。
除了将GeoAddress本身提升回完整的域类外,我如何告诉gorm嵌入使用时不为其生成表?
我在grails-app / domain文件夹中的venue.groovy
class Venue {
String name
LocalDate dateCreated
LocalDate lastVisited
LocalDate lastUpdated
GeoAddress location
Collection posts
static hasMany = [posts:Post]
static embedded =['location']
static constraints = {
lastVisited nullable:true
location nullable:true, unique:true
posts nullable:true
}
static mapping = {
location cascade: "all-delete-orphan", lazy:false, unique:true
posts sorted: "desc", cascade:"save-update"
}
}
class GeoAddress {
String addressLine1
String addressLine2
String addressLine3
String town
String county
String country = "UK"
String postcode
//adds addTo/removeFrom methods to venue
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
}
}
最佳答案
好的-我确实找到了一种方法来使用geoLocation作为嵌入式对象,并阻止它创建表。
我不得不将GeoLoction设置为侧面 field 中的静态内部类。当您执行此操作并运行该应用程序时,您不会在数据库中获得额外的虚假域表。它必须是静态内部类,因为GeoLocation具有用于约束的静态声明。
我不确定这是错误还是什么-但grails文档在5.2节中的建议中是不正确的-gorm中的组合,其中说
If you define the Address class in a separate Groovy file in the grails-app/domain directory you will also get an address table. If you don’t want this to happen use Groovy’s ability to define multiple classes per file and include the Address class below the Person class in the grails-app/domain/Person.groovy file
修改后的类如下所示。当您想创建其中一种格式时(对于bootstrap / tests)看起来像新的Venue.GeoLocation(...)
class Venue implements Serializable {
String name
LocalDate dateCreated
LocalDate lastVisited
LocalDate lastUpdated
GeoAddress location
Collection posts
//static hasOne = [location:GeoAddress] //, temp:TempLocation
static hasMany = [posts:Post] //but doesn't really own thats with user
static embedded =['location']
static constraints = {
lastVisited nullable:true
location nullable:true, unique:true
posts nullable:true
}
static mapping = {
location cascade: "all-delete-orphan", lazy:false, unique:true //eager fetch strategy
posts sorted: "desc", cascade:"save-update"
//comment out for now
//posts joinTable: [name:"venue_posts", key:"venue_id", column:"posts", type:Post]
}
static class GeoAddress {
String addressLine1
String addressLine2
String addressLine3
String town
String county
String country = "UK"
String postcode
//adds addTo/removeFrom methods to venue
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,
matches: /^([gG][iI][rR] {0,}0[aA]{2})|([a-pr-uwyzA-PR-UWYZ](([0-9](([0-9]|[a-hjkstuwA-HJKSTUW])?)?)|([a-hk-yA-HK-Y][0-9]([0-9]|[abehmnprvwxyABEHMNPRVWXY])?)) ?[0-9][abd-hjlnp-uw-zABD-HJLNP-UW-Z]{2})$/
}
}
}
关于grails - grails v3.2.6/gorm嵌入式类在同一域类中。groovy创建虚拟数据库表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42229853/