-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: jpa query that doesn't work
PostPosted: Sun Feb 26, 2012 1:05 pm 
Beginner
Beginner

Joined: Tue Feb 12, 2008 3:53 pm
Posts: 38
Hi,
I use jpql queries using Hibernate

I have an OR clause in jpql query that doen't give me a good result


the following query

SELECT aud.oID FROM eu.eclinica.cdisc.odm.ODMAuditRecord aud WHERE (aud.subjectData is not null and aud.locationRef.location.oID = 1063 and aud.subjectData.clinicalData.study.oID = 1069 and aud.operation = 'INSERT')

gives me the following result ( 1 row)
Code:
1-10382


the following query


SELECT aud.oID FROM eu.eclinica.cdisc.odm.ODMAuditRecord aud WHERE (aud.studyForDeleted.oID= 1069 and aud.locationRef.location.oID= 1063)

give me the following result (2 rows)
Code:
1-10866
2-11370




When I use both conditions with a OR clause

SELECT aud.oID FROM eu.eclinica.cdisc.odm.ODMAuditRecord aud WHERE (aud.subjectData is not null and aud.locationRef.location.oID = 1063 and aud.subjectData.clinicalData.study.oID = 1069 and aud.operation = 'INSERT') OR (aud.studyForDeleted.oID= 1069 and aud.locationRef.location.oID= 1063)


the result is only one row, that is
Code:
1-10382


I removed parts of the first subQuery to find what expression cancels the second subquery and curiously when I remove

and aud.subjectData.clinicalData.study.oID = 1069

I obtain
Code:
1-10866
2-11370
3-10382


I realy don't see why this part of subquery cancels the second subQuery as there is a OR clause.

Thank you in advance for your answer


Top
 Profile  
 
 Post subject: Re: jpa query that doesn't work
PostPosted: Mon Feb 27, 2012 5:00 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Quote:
I use jpql queries using Hibernate


I know HQL, SQLQuery and Query by Criteria.
Can you please explain what you understand with 'jpql'?

N.B.: I suggest you to log the jdbc-statements with p6spy
so you can see how hibernate effectively propagates the query to your database.


Top
 Profile  
 
 Post subject: Re: jpa query that doesn't work
PostPosted: Thu Mar 08, 2012 10:14 am 
Beginner
Beginner

Joined: Tue Feb 12, 2008 3:53 pm
Posts: 38
Hi pb00067 and thank you for your answer. Sorry to answer so late

I meant HQL query

I succeeded in reproducing the same behavior on another query

The complete query QUERY1 is
query = "SELECT a " +
"FROM eu.eclinica.cdisc.odm.ODMAnnotation a " +
"WHERE ((a.subjectData.clinicalData.study.oID = 1069 and a.seqNum = 1) or (a.studyEventData.subjectData.clinicalData.study.oID = 1069 and a.seqNum = 1))"

it gives me 0 result

The query QUERY2 with the first subQuery is
query = "SELECT a " +
"FROM eu.eclinica.cdisc.odm.ODMAnnotation a " +
"WHERE ((a.subjectData.clinicalData.study.oID = 1069 and a.seqNum = 1) "

it gives me 1 result Result1

The query QUERY3 with the second subQuery is
query = "SELECT a " +
"FROM eu.eclinica.cdisc.odm.ODMAnnotation a " +
"WHERE (a.studyEventData.subjectData.clinicalData.study.oID = 1069 and a.seqNum = 1))"

it gives me 1 other result Result2

I remind you that the problem is that the first query QUERY1 should give me 2 results (the result of QUERY2 PLUS the result of QUERY3) as there is a OR clause

I debuged the real request made on the database

Code:
2012-03-08 14:11:37 SQL [DEBUG] select odmannotat0_.oID as oID7_, odmannotat0_.ID as ID7_, odmannotat0_.COMMENTID as COMMENTID7_, odmannotat0_.ANNOTATION_CREDT as ANNOTATION3_7_, odmannotat0_.ANNOTATION_DUEDT as ANNOTATION4_7_, odmannotat0_.FORMDATAID as FORMDATAID7_, odmannotat0_.ITEMDATAID as ITEMDATAID7_, odmannotat0_.ITEMGROUPDATAID as ITEMGRO10_7_, odmannotat0_.SEQNUM as SEQNUM7_, odmannotat0_.EVENTDATAID as EVENTDA11_7_, odmannotat0_.SUBJECTDATAID as SUBJECT12_7_, odmannotat0_.TRANSACTIONTYPE as TRANSACT6_7_ from Annotation odmannotat0_, SubjectData odmsubject1_, ClinicalData odmclinica2_, StudyEventData odmstudyev3_, SubjectData odmsubject4_, ClinicalData odmclinica5_ where odmannotat0_.SUBJECTDATAID=odmsubject1_.oID and odmsubject1_.CLINICALDATAID=odmclinica2_.oID and odmannotat0_.EVENTDATAID=odmstudyev3_.oID and odmstudyev3_.SUBJECTDATAID=odmsubject4_.oID and odmsubject4_.CLINICALDATAID=odmclinica5_.oID and (odmclinica2_.STUDYID=? and odmannotat0_.SEQNUM=1 or odmclinica5_.STUDYID=? and odmannotat0_.SEQNUM=1)


To summarize, the request made on the database is the following (I use there my proper coding)

query = "SELECT a " +
"FROM eu.eclinica.cdisc.odm.ODMAnnotation a " +
"WHERE a.subjectData.clinicalData c1 AND a.studyEventData.subjectData.clinicalData c2 and (c1.study.oID = 1069 and a.seqNum = 1or c2.study.oID = 1069 and a.seqNum = 1)"


I really don't see why the first part is "a.subjectData.clinicalData c1 AND a.studyEventData.subjectData.clinicalData c2"

as these two clause should be used with an "OR". Obviously, seeing the request that is sent to the database it is normal that there is no Results

So how should I implement QUERY1 ?


Top
 Profile  
 
 Post subject: Re: jpa query that doesn't work
PostPosted: Mon Mar 12, 2012 3:10 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
Hi Fabala,

I guess your query is to complicate for the parser and I don't know if there is a way to make the thing work with HQL as unique query.
What surely works is splitting up the query into 2 queries and make a kind of 'union' programmatically:


Code:
query1 = "SELECT a " +
"FROM eu.eclinica.cdisc.odm.ODMAnnotation a " +
"WHERE a.subjectData.clinicalData.study.oID = 1069 and a.seqNum = 1;

query2 = "SELECT a " +
"FROM eu.eclinica.cdisc.odm.ODMAnnotation a " +
"WHERE  a.studyEventData.subjectData.clinicalData.study.oID = 1069 and a.seqNum = 1"

Set<ODMAnnotation> resultset= new HashSet<ODMAnnotation>();
resultset.addAll(query1.list());
resultset.addAll(query2.list());


Top
 Profile  
 
 Post subject: Re: jpa query that doesn't work
PostPosted: Fri Mar 16, 2012 4:13 am 
Beginner
Beginner

Joined: Tue Feb 12, 2008 3:53 pm
Posts: 38
Hi pb00067 and thank you for your answer,

yes indeed I can split up the query into 2 queries

But I am very concerned about this issue and it would really help me if I can see what is wrong in my request.

To your mind, where can I get a precise answer

Thank you again


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.