-->
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: Native SQL Query
PostPosted: Wed Jul 20, 2005 3:36 pm 
Beginner
Beginner

Joined: Thu May 26, 2005 12:31 pm
Posts: 25
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 2.1.8

Name and version of the database you are using: Oracle 9i

Hi,

Our projcet is now completely using Hibernate for all the DB data fetch. But now we got a few new requerements to generate reports which involves a lot of data.

For these reports we have a few SQL queries which invloves joining of 2 or 3 tables and complex where conditions. We may need to add sql hints also for the performance tuning. As we are using hibernate for all other operations, here also we are planning to use Native SQL query feature of Hibernate. I did a basic Native SQLs using hibernate. But I'm not able to go ahead using joining tables and fetching collection of data. Any help will be appreciated. Thanks a lot.

Any code sample using native SQLs (joining tables) will be very helpful.

Sooraj


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 5:46 pm 
Expert
Expert

Joined: Thu Dec 04, 2003 12:36 pm
Posts: 275
Location: Bielefeld, Germany
I think you won't get enough information about joins here. There are several types of joins and things about joins you should know. Try to use google in order to find some resources.

For some reasons you should prefer the ANSI SQL syntax to the "old" style (which uses the join condition in the WHERE clause), but pay attention that your database has to support this standard form of writing joins (e.g. Oracle does not support ANSI SQL joins prior to 9i).

The "old" join style (in this case an inner join) looks like this:
Code:
SELECT e.ename, d.dname
FROM   emp e, dept d
WHERE  e.deptno = d.deptno;


Here is the same example with an ANSI SQL join:
Code:
SELECT e.ename, d.dname
FROM   emp e
JOIN   dept d
ON     e.deptno = d.deptno;


Best regards
Sven


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 9:42 pm 
Beginner
Beginner

Joined: Thu May 26, 2005 12:31 pm
Posts: 25
Thanks for the reply.

I think my query was a bit misleading.

I know native sql joins very well.. but dont how to implement those joins in hibernate.

I've two table

EMP
----

EMP_ID
EMP_NAME

DEPT
--------
DEPT_ID
EMP_ID

and my sql query in oracle sql is

Select Emp.*, DEPT.* from EMP, DEPT where EMP.EMP_ID=DEPT.EMP_ID.

How to implement this in hibernate?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 2:09 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
Assuming your classes are called Emp and Dept, it could be -

List list = session.createSQLQuery("select {e.*}, {d.*} from EMP {e}, DEPT {d} where {e}.EMP_ID={d}.EMP_ID")
.addEntity("e", Emp.class)
.addEntity("d", Dept.class)
.list();

see
http://www.hibernate.org/hib_docs/v3/re ... -scrolling
for explanations.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 3:28 pm 
Beginner
Beginner

Joined: Thu May 26, 2005 12:31 pm
Posts: 25
Wow!! thats really cool.

That link is really helpful.

So if we use multiple table joins in query, is it possible to set where conditions dynamically??

I saw createcriteria.. but will that work with multiple tables?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 5:42 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
If you mean...put in parameters, then yes, that is ...

List list = session.createSQLQuery("select {e.*}, {d.*} from EMP {e}, DEPT {d} where {e}.EMP_ID={d}.EMP_ID" and {e}.EMP_NAME = :name)
.addEntity("e", Emp.class)
.addEntity("d", Dept.class)
.addParameter("name","aname")
.list();


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 21, 2005 8:49 pm 
Beginner
Beginner

Joined: Thu May 26, 2005 12:31 pm
Posts: 25
thanks a lot..
this what I was looking for.


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.