Announcement Announcement Module
Collapse
No announcement yet.
(Hib. 3) HibernateTemplate.load not throwing Exceptions? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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.

    Code:
    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( ObjectNotFoundException.java:27)
    at org.hibernate.event.def.DefaultLoadEventListener.l oad(DefaultLoadEventListener.java:118)
    at org.hibernate.event.def.DefaultLoadEventListener.o nLoad(DefaultLoadEventListener.java:75)
    at org.hibernate.impl.SessionImpl.immediateLoad(Sessi onImpl.java:639)
    at org.hibernate.proxy.AbstractLazyInitializer.initia lize(AbstractLazyInitializer.java:59)
    at org.hibernate.proxy.AbstractLazyInitializer.getImp lementation(AbstractLazyInitializer.java:84)
    at org.hibernate.proxy.CGLIBLazyInitializer.intercept (CGLIBLazyInitializer.java:134)
    at com.SomeObject$$EnhancerByCGLIB$$4ed47797.setDate( <generated>)
    Any ideas?

    van

  • #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.

    Comment


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

      Thanks!

      Comment

      Working...
      X