This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Object Relational Mapping and the fly likes converting from Hibernate to JDBC??? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "converting from Hibernate to JDBC???" Watch "converting from Hibernate to JDBC???" New topic
Author

converting from Hibernate to JDBC???

Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204

I am wondering if their is any easy way to convert from hibernate queries to JDBC queries and visa versa?
Without have to create my own function that converts it.

Is their any function in Hibernate that allows you to take a hibernate query object and convert it to a jdbc sql statement or result set?
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

Sam,
While this question covers both Hibernate and JDBC, I think you are more likely to get an answer in our Hibernate forum. I'll move this for you.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
securetechie techie
Greenhorn

Joined: Nov 18, 2008
Posts: 7
you should close all programs befor hibernate, just like shut down.


Buy SSL certificates today and secure your website. Visit: http://sslstar.com/
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

"securetechie techie" please check your private messages.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Sam Doder wrote:
I am wondering if their is any easy way to convert from hibernate queries to JDBC queries and visa versa?
Without have to create my own function that converts it.

Is their any function in Hibernate that allows you to take a hibernate query object and convert it to a jdbc sql statement or result set?


Well the first is what Hibernate is doing internally so the code that does it is in the Hibenrate source. So if you really need to find out how to do this have a look there. What is it you are trying to achieve by doing this?
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
At work we have tons hibernate stuff and JDBC stuff. What we are trying to do is find a simply way of taking the hibernate query and turning it into a JDBC query. Like taking a Criteria Object and turning it into a PreparedStatment JDBC object ? And visa versa.

Any clues???
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I'm still not sure I understand why you would need to do this? I can't see what benefit this will give you. What is prompting you to make these conversions?


Like taking a Criteria Object and turning it into a PreparedStatment JDBC object ? And visa versa

Like I said, Hibernate aleady does this and the source is available. I'd look at how they do it, or if there is some way to expose the Statement after Hibernate has done its work.

Going the otherway you are probably on your own. JDBC code will contain SQL, there is no SQL parsing element in Hibernate so you can't re-use their code. And SQL can contain way more than HQL or Criteria queries contain. There are Java SQL Parsers out there but I'm not sure how well they will support vendor specific SQL.

Sounds like an awful lot of work.
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
Going the otherway you are probably on your own. JDBC code will contain SQL, there is no SQL parsing element in Hibernate so you can't re-use their code. And SQL can contain way more than HQL or Criteria queries contain. There are Java SQL Parsers out there but I'm not sure how well they will support vendor specific SQL.


That's Ok my main objective was to go from hibernate to jdbc.

But My last resort was to go thru the source. I don't want to have to modify any source or anything.
Their should be away to get the underlying jdbc query for the Critera Object.


Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336


But My last resort was to go thru the source. I don't want to have to modify any source or anything.
Their should be away to get the underlying jdbc query for the Critera Object.

No, not without going through the source. I can't think of a valid reason why the API would need to support this. What have you already achieved through Hibernate than you now have to repeat in JDBC code and why do you need to repeat it?

Remember that Hibernate will also be hitting other things like the Session cache and enlisting in transactions that will not appear in the JDBC code so the JDBC logic will be more complex.
Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
Well it would be nice just getting a sql statement like "select * from customertable where somethingrecord = something;"
For session.createCriteria(Customer.class) ;

Or something. Just to beable to get the query as a string would be nice.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Getting Hibernate to return the SQL do-able - it will log all SQL if you use the hibernate.show_sql property. So if this is all you want I would look in the source for where/how it does this.

Sam Doder
Ranch Hand

Joined: Feb 06, 2008
Posts: 204
Ok , so I found that in hibernate the Query.getQueryString() but how do you get a Query Object from a Criteria Object?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Ok , so I found that in hibernate the Query.getQueryString() ...


I think that will return the HQL behind the query, not the SQL (unless it is a native query).


...but how do you get a Query Object from a Criteria Object?

You can't, there is no API to do it.

Marky Vasconcellos
Ranch Hand

Joined: Jan 28, 2009
Posts: 36
You can still do a SQL query in Hibernate



Look at API:

http://www.dlog.cn/javadoc/hibernate-3.2/doc/api/index.html


Each of their nuggets of wisdom contracted to a sound bite: Joshua Bloch: Write Lots of Code; Chet Haase: Don't Put Your Entire Application in One Method; Masood Mortazavi: Start Simple and Keep Learning; Cay Horstmann: First, Don't Panic
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: converting from Hibernate to JDBC???