Grails标准错误?使用具有复合外键作为id的域:未找到列“ALIAS2X2_.ID”

时间:2012-10-30 11:23:06

标签: grails gorm

问题在于以下查询。我想列出L中包含ID为“I01”的I的所有LI :.

查询:

def c = L.withCriteria {
  lis {
    i {                   
      eq("id","I01")                                 
    }
  }
}
  

找不到列“I_ALIAS2X2_.ID”; SQL语句:选择this_.id为   id4_1_,this_.version为version4_1_,lis_alias1x1_.i_id为i1_7_0_,   lis_alias1x1_.l_id为l2_7_0_,lis_alias1x1_.version为version7_0_   来自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)

模式:

create table I(id varchar(255)not null,version bigint not null,d varchar(255)not null,primary key(id)); create table LI(i_id varchar(255)not null,l_id bigint not null,version bigint not null,primary key(i_id,l_id)); alter table LI添加约束FK97D312CFA外键(i_id)引用I; alter table LI添加约束FK97D328A1A外键(l_id)引用l;

修改

Sérgio的解决方案有效,但如果您有域名我定义如下:

一级{     字符串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 {                   

    }
  }
}

这将给出相同的原始错误。有些事情是不正确的。

1 个答案:

答案 0 :(得分:0)

我在这里遇到了同样的错误。这符合标准,但写了一些不同的作品:

def c = L.withCriteria {
  lis {
    eq('i.id',"I01") 
  }
}