-->
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.  [ 6 posts ] 
Author Message
 Post subject: Complex SQL query in Hibernate
PostPosted: Sat Oct 09, 2004 4:15 pm 
Beginner
Beginner

Joined: Thu Sep 30, 2004 4:17 am
Posts: 21
Hibernate version:
2.1.4

Hibernate's native SQL query support doesn't seem to be supporting "native" sqls. It requires me to specify "hibernate" syntax inside my native query.

Can't I just pass a native sql like:

"SELECT a.col1, a.col2, b.col3, c.col4
from table1 a, table2 b, table3 c
where a....= b.... and b.... = c....
group by ...
order by ..."

to hibernate like:

List list = session.createSQLQuery(<above query>).list();
// list is a collection of Object[]

Can anyone help in this?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 09, 2004 4:24 pm 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
no enough info...

read the doc again, you can do this in hql too using properties and classes...

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 09, 2004 4:38 pm 
Beginner
Beginner

Joined: Thu Sep 30, 2004 4:17 am
Posts: 21
To elaborate on my question: Our native application is using a bunch of complex queries, which I want to cut & paste into Hibernate without making any changes to the query syntax. In otherwords, I don't want to translate SQL queries into Hibernate. Is it possible? If so, any guidance in this would be appreciated.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 09, 2004 5:40 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You can get the JDBC connection from the session and use the query directly through that API. Any other approach will require some modifications to you query. You will not get much benefit from Hibernate but it answers your question.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 10, 2004 2:11 am 
Beginner
Beginner

Joined: Thu Sep 30, 2004 4:17 am
Posts: 21
1. Is there any difference between using the JDBC Connection provided by Hibernate and getting my own Connection through JNDI? Inside a stateless session bean method, I am using Hibernate Session to do some database operations (updates). For operations that cannot be performed by Hibernate (like complex native sqls), I would mix JDBC in the code. Not sure where I am heading to? Especially I am concerned with the Hibernate 1st level cache.

2. Is it possible to wrap the JDBC returned result into Hibernate objects?


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 10, 2004 4:06 am 
Hibernate Team
Hibernate Team

Joined: Thu Dec 18, 2003 9:55 am
Posts: 1977
Location: France
Quote:
1. Is there any difference between using the JDBC Connection provided by Hibernate and getting my own Connection through JNDI? Inside a stateless session bean method, I am using Hibernate Session to do some database operations (updates). For operations that cannot be performed by Hibernate (like complex native sqls), I would mix JDBC in the code. Not sure where I am heading to? Especially I am concerned with the Hibernate 1st level cache.

2. Is it possible to wrap the JDBC returned result into Hibernate objects?


1- why don't you get jdbc connexion from hibernate session ?
You can perfectly use jdbc code.
I advise you to isolate all hibernate & jdbc code inside a DAO layer.
If you mix hibernate & jdbc inside a use case, be sure to flush & clear your hibernate before coding in jdbc.
Now if you use hibernate in one use case, and jdbc in another and you follow the best practices (threadlocal pattern + 1 session per httpRequest), you don't have to worry about the 1st level cache.
If you use second level cache, all classes mapped to tables you'll will hit in pure jdbc shouldn't be cached (i'm talking about second level cache).
2- in this case use hibernate, search for sqlQuery examples, i'm pretty sure you can use it.

_________________
Anthony,
Get value thanks to your skills: http://www.redhat.com/certification


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