named queries are accessed by name, doesn't matter whether they're in xml configuration or annotated in the Java source.
Just use session.getNamedQuery(quer
Main Topics
Browse All TopicsHi folks,
I am evaluating Hibernate for one of our web-projects where it is possible that we need to do a database migration in the future, so I want to keep the Java application code as database-agnostic as possible (no queries in the code, etc). I want to use HQL, because I understand that HQL will allow the queries to be database-independent and that the same queries would work on any database.
Since we don't want any part of the query to be in the code, I guess I need to go with named queries. Now I know that Hibernate treats session.createQuery () as HQL and session.createSQLQuery () as native SQL, but how do we use it in case I have a named query in an HBM file?
Thanks,
Mayank.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
>> I want to use HQL, because I understand that HQL will allow the queries to be database-independent and that the same queries would work on any database.
Consider whether this is a wise constraint. Databases like Oracle have immense power. Limiting yourself to the HQL is a sure way to waste most of the power available to you.
As far as being database agnostic, consider why are you not also Language Agnostic? You chose to go with Java, right?
Java is a specific technology. Not portable to another compiler / VM. Thats technology lockin.
HQL is a specific language. Not portable to another ORM. Thats technology lockin.
But you are not concerned with technology lockin in the app language or ORM. So why be concerned with the db? I say this because I have been involved in many large-scale migrations in my career. The worst performing systems are the ones that don't use the power and features available in the database and try to achieve some mythical platform neutrality. This grail does not exist. I encourage you to use the features that Oracle provides. The data is the heart of your system, so you should be exploiting that technology _first_. I see application rewrites much more often than I see database migrations.
As to Hibernate, you can write those named queries in HQL, yes. In my Hibernate projects I use Hibernate to rapidly develop a working system, then when tuning is needed I will write custom HQL or even lower level JdbcTemplate queries. Hibernate is powerful, but simply will not achieve, in itself, what Oracle is capable of if you exploit it. Try not to use that expensive database as a simple persistence engine. :)
Good luck.
Well, the DB Migration is a decision beyond my reach, and a part of the development of the application has already been started with the use of Hibernate, so I am not allowed to change that. However, the DB migration is a possibility which will come forward due to cost (using Oracle right now but since the cost is high, there is a consideration to migrate to MySQL). As for the application-technology choice, we are going to stick with Java/ Linux for several years right now and there aren't any cost or other constraints which need us to migrate to something else like .NET. So right now, I'm only looking forward to using something database agnostic so that there isn't much logic in the DB queries/ SP's and so that the migration effort is minimal.
I understand. And maybe you misunderstood me, I was not questioning your choice of Java or Hibernate, those are good technologies, but just that you consider:
1) Java and Hibernate are free tools.
2) Oracle is a commercial, powerful database.
If I were to be agnostic about anything it would not be the Oracle database that (1) I paid $$ for and (2) is arguably the best single technology building block for enterprise systems in the world.
I worked for "Big Blue" for years and implemented a large system for a Fortune 500 client every 4-6 months year after year. Every project had one thing in common, a powerful relational database foundation, either DB2, SQL Server or Oracle, but 2 out of 3 times, it was Oracle. I never saw a customer move away from Oracle. I often saw them move to it. When you learn to embrace the database projects become more productive. Database agnostic technologies are good to a point, but are usually heralded by programmers who know little else. Those are not system architects who make good decisions.
Good luck. I am only trying to encourage you to not look on database specific technology as a bad thing.
I understand, but here we are following a hosted model where we are hosting the information - as of now the Oracle license cost is hitting us and the decision to migrate to MySQL is something which is beyond my control because we can't afford Oracle at this point. When the scale grows and the customer base expands, we would be able to afford more Oracle licenses.
Business Accounts
Answer for Membership
by: a_bPosted on 2009-07-21 at 22:16:44ID: 24911730
Here is a short answer to your question - http://www.javalobby.org/j ava/forums /m91885316 .html