Question

The benefits of an ORM are that I can map database tables to Java objects. If I use HQL to write custom statements then what's the point? I may as well write normal SQL queries and use ResultSets?

Was it helpful?

Solution

Well, basically because sometimes it is impossible (or very hard) to write queries using the other alternatives. Then you need to fallback to the HQL alternative.

HQL has, as disadvantage, the fact that you write strings with it. So it is highly recommended other strategies. But, sometimes, it is the fastest way to implement it.

I would also say for you to read this post, which was extensively discussed about HQL already: JPA and Hibernate - Criteria vs. JPQL or HQL

OTHER TIPS

HQL is the only alternative get out the little bit performace left in hibernate.

If you have a big table with many rows and all you need is the id and a description, then you are better of with hql. You can fill a pojo the two column information and if you are using hibernate 4, you have some sort of type safty too.

query:

final TypedQuery<Pojo> query = em.createQuery(new StringBuilder()
.append("SELECT new ").append(Pojo.class.getName())
.append("(tbl.id, tbl.desc) FROM ")
.append(BigTable.class.getName()).append(" tbl ")
.append("WHERE tbl.id between :start and :end ")
.toString(),
Pojo.class
);

List<Pojo> list = query.getResultList();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top