java - Hibernate query "or condition" won't work -
i trying following hibernate query gives me wrong result:
@suppresswarnings("unchecked") @override public list<jcelulasmin> getallothers(int id, string username) { session session = this.sessionfactory.getcurrentsession(); list<jcelulasmin> jcelulaslist = session.createquery("from jcelulasmin jgrelhas.id=? , (jconcorrentes null or jconcorrentes.users.username <> ?) order id").setparameter(0, id).setstring(1, username).list(); for(jcelulasmin p : jcelulaslist){ logger.info("jcelulas list::"+p); } return jcelulaslist; }
this query returns me 13 values, should result of "jconcorrentes.users.username <> ?"
the result of "jconcorrentes null" 47 values query should return 47+13=60...
i'm trying achieve following sql query returns 60 values:
select * `jcelulas` grelhasid=1 , (concorrentesid null or concorrentesid<>1)
ps:jconcorrentes.id 1 sql , hql should same
table/entitiy definition:
@entity @table(name = "jconcorrentes", catalog = "7jogos") public class jconcorrentes implements java.io.serializable { private integer id; .... private users users; @manytoone(fetch = fetchtype.eager) @jsonbackreference @joincolumn(name = "username", unique = true, nullable = false) public users getusers() { return this.users; } public void setusers(users users) { this.users = users; }
i tried following , worked:
session.createquery("from jcelulasmin jgrelhas.id=? , (jconcorrentes null or jconcorrentes <> 1) order id").setparameter(0, id).list();
the problem have username in users.
users:
@entity @table(name = "users", catalog = "7jogos", uniqueconstraints = @uniqueconstraint(columnnames = "username")) public class users implements java.io.serializable { @notempty(message="não se esqueça email") @email(message="email inválido") private string username; ... private set<jconcorrentes> jconcorrenteses = new hashset<jconcorrentes>(0); @onetomany(fetch = fetchtype.eager, mappedby = "users") public set<jconcorrentes> getjconcorrenteses() { return this.jconcorrenteses; } public void setjconcorrenteses(set<jconcorrentes> jconcorrenteses) { this.jconcorrenteses = jconcorrenteses; }
your current query translates inner join between jcelulasmin , jconcorrentes (because of jconcorrentes.users.username
), nulls excluded. have explicitly left join them:
select j jcelulasmin j left join j.jconcorrentes jcon ...
Comments
Post a Comment