Announcement Announcement Module
Collapse
No announcement yet.
Hibernate Types in hibernateTemplate methods Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate Types in hibernateTemplate methods

    Hi,

    I have just migrated an application from Spring 1.1 and Hibernate 2.1 to Spring 1.2 and Hibernate 3. I'm just starting to use hibernate UserTypes and ran into a small problem.

    In 1.1, there was a method on hibernateTemplate called findByNamedQueryAndNamedParam that accepted an array of Hibernate types. This does not seem to be in the Spring 1.2 RC.

    Is there another way of doing this?

    For background information, I'm trying to integrate Joda-Time (replacement for Java's Calendar). I have everything working except for persisting the Joda-Time types. I have a UserType for it now, but in order to use a UserType as criteria in an HQL query, I need to specify the type.

    Any information would be greatly appreciated.

    Thanks,
    Bryan

  • #2
    Something like this?

    public List findEntity(final String queryString, final Object[] values, final Type[] types) throws DataAccessException {
    return getHibernateTemplate().executeFind(new HibernateCallback() {
    public Object doInHibernate(Session session) throws HibernateException {
    Query query = session.createQuery(queryString);
    for (int i = 0; i < values.length; i++) {
    query.setParameter(i, values[i], types[i]);
    }
    return query.list();
    }
    });
    }

    Comment


    • #3
      Thanks,

      I have tried something similar with good results. It is just more convenient to use Spring's hibernateTemplate as it wraps the exceptions, etc.

      Code:
      Type dateMidnightType = Hibernate.custom&#40; PersistentDateMidnight.class &#41;;
      try
      &#123;
          q = this.getSession&#40;&#41;.getNamedQuery&#40; "findFinancialYearByDate" &#41;;
          q.setParameter&#40; "fyStartDate", date, dateMidnightType &#41;;
          q.setParameter&#40; "fyEndDate", date, dateMidnightType &#41;;
      &#125;
      catch &#40; HibernateException e &#41;
      &#123;
          throw getHibernateTemplate&#40;&#41;.convertHibernateAccessException&#40; e &#41;;
      &#125;
      
      return q.list&#40;&#41;;

      Comment


      • #4
        It is just more convenient to use Spring's hibernateTemplate as it wraps the exceptions
        Not sure if you are referring to your code post or alesj's. Using the HibernateCallback (as in alesj's post) will wrap exceptions automatically. You can then remove the boilerplate code:
        Code:
        catch &#40; HibernateException e &#41;
        &#123;
            throw getHibernateTemplate&#40;&#41;.convertHibernateAccessException&#40; e &#41;;
        &#125;

        Comment


        • #5
          I understand now. I didn't read the previous code carefully enough.

          Thanks for the help.

          Comment


          • #6
            Here's some more info and some more options, if you are still interested:
            http://opensource.atlassian.com/proj...rowse/SPR-1159
            http://www.springframework.org/docs/...m.html#d0e6121

            I have the same problem querying a DB2 DATE column, and worked around it with a HibernateCallback.

            Comment

            Working...
            X