Oleksandr
Oleksandr

Reputation: 3801

SQL\HQL queries repository

While working on some java projects i've saw some sort of SQL repository.

The idea was to place all queries in one(or few) xml files and retrieve them when needed by name. Something like this:

String sql = getSQLRepository().getSQL("SELECT_ALL_ROWS", params)
String sql2 = getSQLRepository().getSQL("SELECT_ROWS_WITH_COND", params)

In my current Grails project i have a lot of HQL queries in dozens of classes and it's hard to track them all. It seems that HQL repository would be very nice solution.

So could anyone tell if some sort of SQL\HQL repository implementation allready present or there are better solutions present ?

Upvotes: 1

Views: 1135

Answers (4)

kan
kan

Reputation: 28981

After we started use the http://source.mysema.com/display/querydsl there is no need to think about text queries and how to manage them.

Upvotes: 2

javagirl
javagirl

Reputation: 1675

I'm sorry, and it doesn't relate to Hibernate, but when I worked with iBatis, - there are the situation as you are writing about exactly. A few xml (partially generated itself) files, containing SQL queries, which was easily to use in DAO

Upvotes: 0

01es
01es

Reputation: 5410

Have a look at Mapping Queries in Hibernate reference.

Upvotes: 2

AlexR
AlexR

Reputation: 115378

I'd recommend you to use the good old properties files. You can put them into your classpath and then use as following:

Properties sql = new Properties();
properties.load(getClass().getResourceAsStream("sql.properties"));
////////
String query = sql.get("SELECT_ALL_ROWS");

Upvotes: 1

Related Questions