Announcement Announcement Module
No announcement yet.
HibernateTemplate supports uniqueResult() and query caching? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • HibernateTemplate supports uniqueResult() and query caching?

    It would be nice if hibernate template can support retrieving a single persisted object instead of a list. Also, enabling query cache by the template directly (not through its created Query object ) would also be nice.

    I didn't find them from the API, or did I just miss them :?: ?

  • #2
    Regarding query cache, there's HibernateTemplate's "cacheQueries" bean property, which will apply to all queries executed by the template.

    Regarding retrieving a single persistent object, there are no such built in methods in HibernateTemplate, mainly for the reason that there are a lot of find signature variations.

    As an alternative, you can use the generic class, like as follows, which is pretty concise too:

    Object myObject = DataAccessUtils.uniqueResult(getHibernateTemplate().find(...));
    The uniqueResult method will throw proper DataAccessExceptions if the given collection size does not match.