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.
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?Code: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 " + "p.id = :productid"; Query query = session.createQuery(queryString); query.setParameter("productId", productId); query.setParameter("desc", desc); query.setParameter("price", price); query.setParameter("title", title); query.executeUpdate(); return null; } }); return; }
Thanks in advance for hints and help!![]()


Reply With Quote