Grails criteria bug? with domain that has composite foreign key as id : Column “ALIAS2X2_.ID” not found

StackOverflow https://stackoverflow.com/questions/13137954

  •  21-07-2021
  •  | 
  •  

Question

Problem is with the following query. I want to list all the LI:s in L that contain an I with id 'I01'.

query:

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

Column "I_ALIAS2X2_.ID" not found; SQL statement: 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]

Is there something wrong in my criteria or are my domains not correct ? If I add 'long id' into LI domain and comment out the 'id: composite...' row then criteria runs ok.

domains:

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 = {
}
}

bootstrap:

    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)

schema:

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 add constraint FK97D312CFA foreign key (i_id) references I; alter table LI add constraint FK97D328A1A foreign key (l_id) references l;

EDIT:

Sérgio's solution works but if you have domain I defined like this:

class I { String id

static belongsTo=[A:a] //has only 'string id' column

static mapping = {   
    table "I"       
    id generator:'assigned'
    version: false   
}   
static constraints = {
}
}

Then this won't work:

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

if you write just:

def c = L.withCriteria {
  lis {
    i {                   

    }
  }
}

this will give the same original error. Something is not correct.

Était-ce utile?

La solution

I've got the same error here. It's something on the criteria, but writing a little bit different works:

def c = L.withCriteria {
  lis {
    eq('i.id',"I01") 
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top