• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

converting from Hibernate to JDBC???

 
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you should close all programs befor hibernate, just like shut down.
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"securetechie techie" please check your private messages.
 
Paul Sturrock
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can still do a SQL query in Hibernate



Look at API:

http://www.dlog.cn/javadoc/hibernate-3.2/doc/api/index.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic