Announcement Announcement Module
No announcement yet.
Generic queries HibernateDAO Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Generic queries HibernateDAO

    I'm trying to figure out the right way to do generic hibernate queries in Spring. I'm just starting out with this, so please be forgiving about any very obvious mistakes.

    It appears that HibernateTemplate is useful for a number of basic type of queries, but as soon as things start to get a little more specific, using HibernateCallback/doInHibernate directly appears to be more common. The sheer amount of code to perform a queries this way looks overwhelming to me though, so I'm wondering if I'm doing this correctly.

    Would anyone care to take a look at the sample query below and give me some feedback on whether this is appropriate use of hibernate in spring? This is intended to be used with a Spring/Hibernate/AOP type of setup, BTW, so I'm not sure this is handling the session appropriately.
    public void updateProduct(Product product) {
            final String title = product.getTitle(); 
            final String desc = product.getDescription();
            final BigDecimal price = product.getPrice();
            final int productId = product.getId();
            getHibernateTemplate().execute(new HibernateCallback() {
                public Object doInHibernate(Session session) throws JDBCException {
                    String queryString =
                        "update "
                            + "Product p set Title = :title, Description = :desc, Price = :price"
                        + "where "
                            + " = :productid";
                    Query query = session.createQuery(queryString);
                    query.setParameter("productId", productId);
                    query.setParameter("desc", desc);
                    query.setParameter("price", price);
                    query.setParameter("title", title);
                    return null;
    Moreover, is my idea that HibernateTemplate is a general helper class for generic queries vs the direct use of HibernateCallback for very specific/complex queries correct? And, in general, what is the appropriate way to do queries using Spring/Hibernate AOP?

    Thanks in advance for hints and help!
    Last edited by ultron; Jun 13th, 2007, 07:26 AM.

  • #2
    Couldn't you use this instead?[])
    Last edited by karldmoore; Aug 30th, 2007, 05:26 AM.


    • #3
      You are right. You use hibernate template methods for standard simple queries and hibernate callback for more complex or more generic queries.
      For example I have many cases where the query is the same except the order-by clauses succession. It is convenient then to code a generic query using hibernate callback and Criteria and add the ordering dynamicaly.

      A quick look on your code tells me that your query is a simple one. Assuming your hibernate mappings are ok you can do the same thing with a single line of code, just call the saveOrUpdate() method of hibernate template.


      • #4
        If you are working with a persistent entity you don't even need to do that.
        Last edited by karldmoore; Aug 30th, 2007, 05:26 AM.


        • #5

          @NamedQuery(name="Book.findByName" query="FROM Book WHERE name = ?")
          public class Book extends AbstractEntity<Long>  {
             private String name; // accessors for hibernate
          public interface BookDAO extends DAO<Book>  {
              List<Book> findByName(String name);
          public interface BookService extends Service<Book>  {
              List<Book> searchBooks(String name);
          public class BookServiceImpl extends AbstractServiceImpl<Book> implements BookService  {
              private BookDAO bookDAO; // setter for spring
              public BookDAO getDAO()  {
                  return bookDAO;
              public List<Book> searchBooks(String name)  {
                  return getDAO().findByName(name);
          notice all of the lower level hibernate stuff like setting the parameters is all up in the super class. This approach could be kind of limited, but you can always subclass the DAO implementation to run criterias etc. But if you have a bunch of finder methods, this is pretty nice. The most typing you'll have to do is the spring config. I even wrote some factories to cut that down, that article is pretty damn good and has worked very well for us.

          just looked at your code and noticed what you are actually doing. hibernate does the inserts & updates for you man, the only sql like stuff you'll write is to pull things out. And the criteria api is often more useful than HQL.
          Last edited by lloyd.mcclendon; Jun 14th, 2007, 10:20 PM.