Announcement Announcement Module
No announcement yet.
(Hib. 3) HibernateTemplate.load not throwing Exceptions? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • (Hib. 3) HibernateTemplate.load not throwing Exceptions?

    I have a class that subclasses HibernateDaoSupport. When the following fragment inside that class is called and there is no object with the given id, no Exceptions are thrown.

    public Object (Class class, Integer id) {
            try {
                return getHibernateTemplate().load(clazz, id);
            catch (RuntimeException e) {
                if (e.getCause() != null && e.getCause().getClass().equals(ObjectNotFoundException.class)) {
                    return null;
                throw e;
    Later, when I try to call setDate() on the returned Object, I get something like this:

    org.hibernate.ObjectNotFoundException: No row with the given identifier exists: [com.SomeObject#78]
    at org.hibernate.ObjectNotFoundException.throwIfNull(
    at org.hibernate.event.def.DefaultLoadEventListener.l oad(
    at org.hibernate.event.def.DefaultLoadEventListener.o nLoad(
    at org.hibernate.impl.SessionImpl.immediateLoad(Sessi
    at org.hibernate.proxy.AbstractLazyInitializer.initia lize(
    at org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(
    at org.hibernate.proxy.CGLIBLazyInitializer.intercept (
    at com.SomeObject$$EnhancerByCGLIB$$4ed47797.setDate( <generated>)
    Any ideas?


  • #2
    Read the javadocs of hibernate Session, mainly the differences between load and get. In short, load will never throw a session even if the object you are trying to load doesn't exist because it returns a session. Thus an exception will be return when you try to do some operations on the returned session because when HB tries to resolve the proxy it will not find anything and throw an error.
    Session.get on the other had returns null in case it does not find any object with the given identifier.


    • #3
      Changing to get() did the trick.