我想对Domain关联做反思,为RESTful WS(脚手架)自动生成JSON / XML格式类描述符文件。在HasOne(假设是双向)的情况下,我试图使用Association对象中的referencedPropertyName来显示反向键(这是WS必须显示的唯一信息)。
然而,我发现hasOne关联未正确初始化。
以下是我使用Grails 2.3.7进行的测试。
我的域名是:
class House {
Long number
Integer inhabitants
static hasOne = [ roof: Roof ]
static hasMany = [ doors: Door ]
static constraints = {
roof nullable: true, unique: true
}
}
class Roof {
String color
House house
static constraints = {
}
}
然后:
import org.grails.datastore.mapping.model.types.Association
import org.junit.Test
import spock.lang.*
import test.House
class AssociationsSpec {
@Test
void "test something"() {
House.gormPersistentEntity.associations.each { Association association ->
String key = association.name
println association.properties
assert association.bidirectional
}
}
}
此测试失败:
Assertion failed:
assert association.bidirectional
| |
| false
test.House->roof
at test.AssociationsSpec$_test_something_closure1.doCall(AssociationsSpec.groovy:26)
at test.AssociationsSpec.test something(AssociationsSpec.groovy:23)
其他信息(println association.properties):
[list:false, type:interface java.util.Set, owner:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@149a797, class:class org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity$5, cascadeOperations:[ALL], inverseSide:test.Door->house, capitilizedName:Doors, bidirectional:true, referencedPropertyName:house, associatedEntity:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@99a9c3, mapping:null, circular:false, owningSide:true, name:doors, fetchStrategy:EAGER]
[capitilizedName:Roof, bidirectional:false, referencedPropertyName:null, circular:false, owningSide:true, name:roof, list:false, type:class test.Roof, owner:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@149a797, class:class org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity$4, cascadeOperations:[ALL], inverseSide:null, associatedEntity:org.codehaus.groovy.grails.domain.GrailsDomainClassPersistentEntity@16289cc, mapping:null, foreignKeyInChild:true, fetchStrategy:EAGER]
谢谢
答案 0 :(得分:3)
要使此双向,您需要添加static belongsTo = [house: House]
并从House house
域中删除Roof
媒体资源。
您可以在documentation。
中详细了解belongsTo属性答案 1 :(得分:0)