问题在于以下查询。我想列出 L 中所有包含 ID 为“I01”的 I 的 LI:s。
询问:
def c = L.withCriteria {
lis {
i {
eq("id","I01")
}
}
}
未找到“I_ALIAS2X2_.ID”列;SQL语句:select this_.id as id4_1_, this_.version as version4_1_, lis_alias1x1_.i_id as i1_7_0_, lis_alias1x1_.l_id as l2_7_0_, lis_alias1x1_.version as version7_0_ from l this_ inner join LI lis_alias1x1_ on this_.id=lis_alias1x1_.l_id where ( (i_alias2x2_.id=?)) [42122-164]
我的标准有问题还是我的域不正确?如果我将“long id”添加到 LI 域并注释掉“id:composite ...”行,则标准运行正常。
域:
class L {
long id
//can this hasMany be used here ? domain L is the other FK in LI domain
static hasMany = [lis: LI]
static mapping = {
lis: joinTable: false
}
static constraints = {
}
}
import org.apache.commons.lang.builder.HashCodeBuilder
class LI implements Serializable {
//domain has only FK:s to L and I
static belongsTo = [l: L, i: I]
static mapping= {
table "LI"
id composite:['i', 'l']
i column: 'i_id'
l column: 'l_id'
}
static constraints = {
}
boolean equals(other) {
if (!(other instanceof LI)) { return false }
other.l == l && other.i == i
}
int hashCode() {
def builder = new HashCodeBuilder()
builder.append l
builder.append i
builder.toHashCode()
}
}
class I {
String id
static mapping = {
table "I"
id generator:'assigned'
version: false
}
static constraints = {
}
}
引导程序:
I ii = new I(id:"I01").save(flush:true)
I ii2 = new I(id:"I02").save(flush:true)
L l = new L().save(flush:true);
L l2 = new L().save(flush:true);
LI li = new LI(l:l,i:ii).save(flush:true)
LI li2 = new LI(l:l2, i:ii2).save(flush:true)
架构:
创建表 I (id varchar(255) not null, version bigint not null, d varchar(255) not null, 主键 (id)); 创建表 LI (i_id varchar(255) not null, l_id bigint not null, version bigint not null, 主键 (i_id, l_id)); 更改表 LI 添加约束 FK97D312CFA 外键 (i_id) 引用 I;更改表 LI 添加约束 FK97D328A1A 外键 (l_id) 引用 l;
编辑:
Sérgio 的解决方案有效,但如果您有我这样定义的域:
类 I { 字符串 id
static belongsTo=[A:a] //has only 'string id' column
static mapping = {
table "I"
id generator:'assigned'
version: false
}
static constraints = {
}
}
那么这将不起作用:
def c = L.withCriteria {
lis {
i {
eq("a.id","A01")
}
}
}
如果你只写:
def c = L.withCriteria {
lis {
i {
}
}
}
这将给出相同的原始错误。有些事情不正确。