mysql - MySQLSyntaxErrorException generated by Hibernate when using count of children in query -
i have query in want retrieve message objects have 1 or more link children.
my message class is:
package zzz.community.domain; /** * entity implementation class entity: message. */ @entity @table(name = "message") public class message implements serializable { private static final long serialversionuid = 2252587276028343749l; public static final int max_length_title = 255; public static final int max_length_text = 200000; @id @generatedvalue private long id; private string title; @column(columndefinition = "text") private string text; /** * date @ message posted. */ private date creationdate; /** * profile id of user posted message. */ @onetoone private profile creator; /** * id of circle message posted to. */ @onetoone private circle circle; /** * when true, members of circle may see message. */ private boolean membersonly; @onetomany(mappedby="message") @orderby("id desc") private set<link> links; public long getid() { return id; } public void setid(final long id) { this.id = id; } public string gettext() { return text; } public void settext(final string text) { this.text = text; } public string gettitle() { return title; } public void settitle(final string title) { this.title = title; } public date getcreationdate() { return creationdate; } public void setcreationdate(final date creationdate) { this.creationdate = creationdate; } public profile getcreator() { return creator; } public void setcreator(final profile creator) { this.creator = creator; } public circle getcircle() { return circle; } public void setcircle(final circle circle) { this.circle = circle; } public boolean getmembersonly() { return membersonly; } public void setmembersonly(final boolean membersonly) { this.membersonly = membersonly; } public set<link> getlinks() { return this.links; } public void setlinks(final set<link> links) { this.links = links; } public void addlink(final link link) { this.links.add(link); } }
the jpa query wrote is:
select m message m m.circle = :circle , count(m.links) > 0 order m.creationdate desc
hibernate parses code correctly (no ast exception) resulting mysql statement not correct , throws mysqlsyntaxerrorexception
09:01:22.153 [http-apr-8080-exec-3] debug org.hibernate.hql.ast.errorcounter - throwqueryexception() : no errors 09:01:22.154 [http-apr-8080-exec-3] debug o.h.hql.ast.querytranslatorimpl - hql: select m zzz.domain.message m m.circle = :circle , count(m.links) > 0 order m.creationdate desc 09:01:22.154 [http-apr-8080-exec-3] debug o.h.hql.ast.querytranslatorimpl - sql: select message0_.id id3_, message0_.circle_id circle6_3_, message0_.creationdate creation2_3_, message0_.creator_id creator7_3_, message0_.membersonly memberso3_3_, message0_.text text3_, message0_.title title3_ message message0_ cross join link links1_ message0_.id=links1_.message_id , message0_.circle_id=? , count(.)>0 order message0_.creationdate desc
again, resulting mysql query:
select message0_.id id3_ , message0_.circle_id circle6_3_ , message0_.creationdate creation2_3_ , message0_.creator_id creator7_3_ , message0_.membersonly memberso3_3_ , message0_.text text3_ , message0_.title title3_ message message0_ cross join link links1_ message0_.id=links1_.message_id , message0_.circle_id=? , count(.)>0 order message0_.creationdate desc
the workaround, of course, remove selection criterion
count(m.links) > 0
and replace code in java. hope better suggestion of 1 of you.
in advance,
henk
in jpql can done example 1 of following constructs:
- empty collection comparison expression [not] empty
- size function
with first 1 following:
count(m.links) > 0
is replaced by:
m.links not empty
and second approach is:
size(m.links) > 0
Comments
Post a Comment