hibernate - Grails: Criteria query fail on join tables? -
we have problem working grails (2.3.7), it's related criteria queries , mappings. problem after defining criteria , trying list results, hibernate seems not adding mapping tables query, , therefore, query fails.
right have 3 tables related: users, competitions + usercompetition has relationship among other 2 tables composite primary key, has reference each 2 tables foreign key.
here there definition of our domains:
class user { (...) static hasmany = [competitions:competition] static mapping = { table "users" (...) competitions jointable: [name: 'userscompetitions', key: 'userid', column: 'competitionid'] } class competition { (...) static hasmany = [users:user] static belongsto = user static mapping = { table "competitions" id column: 'competitionid' users jointable: [name: 'userscompetitions', key: 'competitionid', column: 'userid'] } } class usercompetition implements serializable { private static final long serialversionuid = 1 competition competition user user boolean planning static mapping = { table "userscompetitions" id composite: ['user', 'competition'] competition column: 'competitionid' user column: 'userid' } boolean equals(other) { if (!(other instanceof usercompetition)) { return false } other.user?.id == user?.id && other.competition?.id == competition?.id } int hashcode() { def builder = new hashcodebuilder() if (user) builder.append(user.id) if (competition) builder.append(competition.id) builder.tohashcode() } static usercompetition get(long userid, long competitionid) { usercompetition.where { user == user.load(userid) && competition == competition.load(competitionid) }.get() }
}
and finally, trying retrieve data usercompetitions using criteria:
def c = usercompetition.createcriteria() def competitions = c.list{ createalias("competition", "comp") and{ isnotnull("comp.startdate") gt("comp.startdate", now) } eq("user.id",user.id) ne("planning",true) maxresults(3) order("comp.startdate", "asc") }
that's get:
message: unknown column 'comp1_.startdate' in 'where clause'
and query generated hibernate , can see, didn't add relation competition table:
select this_.userid userid0_0_, this_.competitionid competit2_0_0_, this_.planning planning0_0_ userscompetitions this_ (comp1_.startdate not null , comp1_.startdate>?) , this_.userid=? , this_.planning<>? order comp1_.startdate asc limit ?
we trying understand whats going on, not able understand why hibernate not adding mapping table query :-?
thanks in advance,
the problem related how grails interpreting mappings uses jointable
.
we have deleted jointable
s references inside user
, competition
domain classes , linking them through usercompetition
instead of each other , queries started work.
class user { (...) static hasmany = [competitions:usercompetition] static mapping = { table "users" (...) /* competitions jointable: [name: 'userscompetitions', key: 'userid', column: 'competitionid'] */ } class competition { (...) static hasmany = [users:usercompetition] static belongsto = user static mapping = { table "competitions" id column: 'competitionid' /* users jointable: [name: 'userscompetitions', key: 'competitionid', column: 'userid'] */ } }
Comments
Post a Comment