Results 1 to 3 of 3

Thread: (Hib. 3) HibernateTemplate.load not throwing Exceptions?

  1. #1
    Join Date
    May 2005
    Posts
    2

    Default (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. #2
    Join Date
    Jan 2005
    Location
    Bucharest, Romania
    Posts
    5,403

    Default

    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.
    Costin Leau
    SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
    http://twitter.com/costinl
    Please use [ c o d e ] [ / c o d e ] tags

  3. #3
    Join Date
    May 2005
    Posts
    2

    Default

    Changing to get() did the trick.

    Thanks!

Similar Threads

  1. Replies: 7
    Last Post: May 18th, 2005, 04:38 PM
  2. Replies: 3
    Last Post: May 18th, 2005, 10:07 AM
  3. Replies: 2
    Last Post: Apr 20th, 2005, 06:25 AM
  4. Replies: 2
    Last Post: Mar 29th, 2005, 07:45 AM
  5. Replies: 2
    Last Post: Dec 20th, 2004, 04:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •