File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JDBC and Relational Databases and the fly likes Externalizing SQL statements in JEE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Externalizing SQL statements in JEE" Watch "Externalizing SQL statements in JEE" New topic

Externalizing SQL statements in JEE

Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
Hello all:

We are using Hibernate as an ORM to build a JEE application.
I want to know if putting the HQL in an external file is a good idea.
If it is, why? and how it can be implemented?


SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
Jan Cumps

Joined: Dec 20, 2006
Posts: 2562

I don't do it. To my feeling, the queries belong to the code. So I want them as close to the code as possible. It makes things easier to understand.
I do externalize internationalization-sensitive things, like messages.

There are other opinions around.

If you want to externalize your queries, you could use Property files or a ResourceBundle.

Regards, Jan
[ April 27, 2007: Message edited by: Jan Cumps ]

OCUP UML fundamental and ITIL foundation
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32819

It's a matter of personal preference.

I find it useful to put things that might change, like the schema name, in a property file. If the queries are highly static, it could make sense to put them in a property file.

I tend to do more dynamic queries and like them closer to the code. Also, if I change the query, I usually need to change the code (prepared statement or resultset), so it's helpful to have them in the same place.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
I agree. Here's the link:
subject: Externalizing SQL statements in JEE
It's not a secret anymore!