-->
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.  [ 3 posts ] 
Author Message
 Post subject: how do I do a boolean and() query?
PostPosted: Thu Aug 26, 2004 7:24 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I have a one-to-many association from table A to B. B has a boolean field called is_accessed. I'd like to write a query that gives me all the A's where all it's own B's are true (or false), but I'm having a real hard time doing this. Is there anyway I can do something like:

Code:
select a
from A a inner join a.bs b
where andOperation(b.isRead) =
   (select b
    from B b
    where b.a = a
   )


Thanks for your help.


Top
 Profile  
 
 Post subject: Re: how do I do a boolean and() query?
PostPosted: Thu Aug 26, 2004 9:47 am 
Beginner
Beginner

Joined: Mon Jul 26, 2004 4:29 pm
Posts: 45
Location: TX, USA
egervari wrote:
I have a one-to-many association from table A to B. B has a boolean field called is_accessed. I'd like to write a query that gives me all the A's where all it's own B's are true (or false), but I'm having a real hard time doing this.


I have two tables: Department and Employee with a one-to-many from Department to Employee. I have a char(1) field in Employee which could be 'Y' or 'N' (but not null). In my case, this works to get all the Departments where all of the department employees are 'Y':

Code:
    Iterator deptIterator = session
        .iterate("from Department as dept where exists "
            + "(select emp from Employee as emp "
            + "where emp.active = 'Y' and emp.dept = dept.id)"
            + "and not exists (select emp from Employee as emp "
            + "where emp.active = 'N' "
            + "and emp.dept = dept.id)");


HTH,
Maury


Top
 Profile  
 
 Post subject: Re: how do I do a boolean and() query?
PostPosted: Thu Aug 26, 2004 11:40 am 
Senior
Senior

Joined: Sun Oct 26, 2003 5:05 am
Posts: 139
I went with this using the exists() keyword as well. I couldn't get it to work any other way. Since I already have the total, I can get the read histories just by subtracting from the unread. Now I have to include this in my other reporting query if I can, although this one is sort long as it is :/

Code:
               Query query = session.createQuery(
                   "select count(distinct rnsTransaction) " +
                  "from Administrator administrator " +
                  "   inner join administrator.businesses business " +
                  "   inner join administrator.persons person " +
                  "   inner join business.rnsTransactions rnsTransaction " +
                  "   inner join rnsTransaction.histories history " +
                  "where " +
                  "   exists (" +
                  "      select history " +
                  "      from RnsTransactionHistory history " +
                  "      where " +
                  "         history.rnsTransaction = rnsTransaction and " +
                  "         history.isRead = false " +
                  "   ) and " +
                  "   business.id = :businessId and " +
                  "   person.id = :personId "
               );


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.