-->
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.  [ 7 posts ] 
Author Message
 Post subject: exists options
PostPosted: Thu May 06, 2004 3:16 pm 
Newbie

Joined: Mon May 03, 2004 1:08 am
Posts: 18
Is there a way to write the exists query?

salesman( ssn)
sale(carvin, model , ssn, month)
employeeofMotnh(ssn,month)

Query A) select all salesman who sold Taurus (which is the model) .
I need distinct values.


s.createQuery(
from salesman man
where exists ( from sale as sold
where sold.ssn = man.ssn )


This will work. However this is very close to SQL .


My queries will need to be dynamically build . Fo example the query criteria could be Query A + the salesman should be employee of month
(If salesman is employee of month and sold a a taurus car)

Is there a better way of writing this? I need to make this easier for developers not intimately familar with SQL

thanks


Top
 Profile  
 
 Post subject: exists ?
PostPosted: Fri May 14, 2004 1:09 pm 
Newbie

Joined: Mon May 03, 2004 1:08 am
Posts: 18
Is exists supported ?
I want to use Hibernate to make things easier for Java developers without making them write SQL

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 1:23 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
thinking object and understanding HQL, i think you won't have any problem to query this.


Top
 Profile  
 
 Post subject: example-
PostPosted: Fri May 14, 2004 2:58 pm 
Newbie

Joined: Mon May 03, 2004 1:08 am
Posts: 18
Can you please give me an example in HQL.

Show me all departments where there is an employee whose name is Mark


Class Dept( deptName)
Class Emp(ssn,empName, deptName)


Assumptions-
I want unique departments .
LeftOuterJoin gives me duplicates.
I am on a webpage so need paging (first 5 , next 5 rectords etc)
Ordering.
(So I am reluctant to use UniqueResult() for performance reason and not sure if paging will be incorrect )



Any help or pointers will be greatly appreciated


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 14, 2004 4:07 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
If you need to build dynamic queries, use the criteria API:

Code:
session.createCriteria(Salesman.class)
    .createCriteria("employees")
          .add(Expression.eq("name", "marc")
    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
    .setMaxResults(5)
    .setFirstResult(5)
    .list();


Ah yes, and start reading the docs and the API.


Top
 Profile  
 
 Post subject: Incorrect result
PostPosted: Mon May 17, 2004 4:31 pm 
Newbie

Joined: Mon May 03, 2004 1:08 am
Posts: 18
Please let me know if I am doing something incorrectly here-


select name
from salesman
where exists
(select 1 from sale
where salesman.ssn= sale.ssn
and sale.model='Taurus');
Order by name;


Results in this case is
-----------------------------
Marc
Sam


Lets say Marc sold 100 taurus
sam sold 5 cars



tx = s.beginTransaction();
List list1 = s.createCriteria(salesman.class)
.createCriteria("sale", "sale")
.add( Expression.eq("model", "Tauras") )
.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)
.setMaxResults(5)



Doing the same in Hibernate HQL as suggested will give incorrect result.
Why Marc sold 100 cars , in the join between salesman and sale there are
100 records for marc and 5 for sam.
Limiting results to 5 will return the first 5 records. (Not what I want)


exists is supposed to return a unique value of salesman in the sql query.
Doing a Criteria.DISTINCT_ROOT_ENTITY will filter the results after the query is executed .
Again if I want to show the first 5 salesman then the next 5 etc the results will all be incorrect.


Is exists supported in Hibernate?
thanks


Top
 Profile  
 
 Post subject:
PostPosted: Mon May 17, 2004 5:52 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Well in that case there is no other way than the "working one" you posted.


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