我有一个一元对多关系,需要在Grails上进行映射:
ACCESS_RIGHT
可能需要先获得多个其他ACCESS_RIGHT
才能被授予。ACCESS_RIGHT
可能有多个需求和多个从属。ACCESS_RIGHT
成为另一个ACCESS_RIGHT
的要求。我在Grails应用程序中为其使用了一个联接表。
AccessRight {
String id
String name
String description
static hasMany = [
requirements: Dependency,
dependents: Dependency
]
}
Dependency {
String id
Date effectivityDate
static belongsTo = [
dependent: AccessRight,
requirement: AccessRight
]
}
问题是Hibernate自动映射的列是错误的。考虑以下示例:
AccessRight rights = AccessRight.findById(id)
// select
// this_.id as id1_4_3_,
// this_.name as name2_4_3_,
// this_.description as descript3_4_3_
// from
// access_right this_
// where
// this_.id = ?
LinkedHashSet<Dependecy> listOfDependents = rights.getDependents()
// select
// dependenci0_.dependent_id as access_r2_4_0_,
// dependenci0_.id as id1_5_0_,
// dependenci0_.id as id1_5_1_,
// dependenci0_.dependent_id as dependen2_5_1_,
// dependenci0_.effectivity_date as effectiv2_5_2_,
// dependenci0_.requirement_id as require3_5_1_
// from
// dependency dependenci0_
// where
// dependenci0_.dependent_id = ?
LinkedHashSet<Dependecy> listOfRequirements = rights.getRequirements()
// select
// dependenci0_.dependent_id as access_r2_4_0_,
// dependenci0_.id as id1_5_0_,
// dependenci0_.id as id1_5_1_,
// dependenci0_.dependent_id as dependen2_5_1_,
// dependenci0_.effectivity_date as effectiv2_5_2_,
// dependenci0_.requirement_id as require3_5_1_
// from
// dependency dependenci0_
// where
// dependenci0_.dependent_id = ?
请注意,调用.getDependents()
时,它将使用Dependency
列查询dependent_id
表。但是,在调用.getRequirements()
时,它也使用相同的requirement_id
而不是使用dependent_id
进行连接。是什么赋予了?有没有办法手动声明要在hasMany
属性上使用哪一列?
我目前正在使用以下方法来检索记录。
List<Dependency> getDependents() {
return Dependency.createCriteria().list {
eq("requirement", this)
}
}
List<Dependency> getRequirements() {
return Dependency.createCriteria().list {
eq("dependent", this)
}
}
问题是我无法将FetchMode.EAGER用于这种设置,这是我将来真正需要的。