-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Catch exceptions in findById mwthod of HibernateGenericDAO
PostPosted: Mon Mar 13, 2006 5:30 am 
Beginner
Beginner

Joined: Mon Jan 24, 2005 11:56 am
Posts: 24
Hi,

We have updated the GenericHibernateDAO findById method to catch ObjectNotFoundException and re-throw some custome exception:
Code:

    public T findById(ID id, boolean lock) throws DataAccessException {
        T entity;
        try {
            if (lock)
                entity = (T) getSession().load(getPersistentClass(), id, LockMode.UPGRADE);
            else
                entity = (T) getSession().load(getPersistentClass(), id);

        } catch (ObjectNotFoundException ex) {
            log.debug("Could not find object by " + id);
            throw new DataAccessException(DataAccessError.NOT_FOUND);
        }
        return entity;
    }



It does not catch the excetion! We replaced it with java.lang.Throwable nothing happens and the catch does not catch any thing. Should we catch the exception some where else?

The signature of Session.load method shows that it throws HibernateException. The inersting thing is that when we remove the try/catch block the compiler does not show any complie errors and does NOT complain about uncoughted exceptions!

Seems to be tricky to us, any comments!

Regards,
Alireza Fattahi


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 8:07 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
I also don't like this behaviour. According to me, the exception should be thrown in hibernate's "load" method, but it doesn't. The exception gets thrown from the moment you perform an action with an object that's not found.

Object notfound = someDAO.findByID(Object.class, id); <-- doesn't throw, but should (according to me)
SomeValue value = notfound.getSomeValue(); <-- does throw the exception. WAY too late.

_________________
Don't forget to rate my post if it helped. :)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Apr 11, 2006 8:12 am 
Beginner
Beginner

Joined: Wed Apr 05, 2006 3:19 pm
Posts: 43
Now I know why this happens: the .load method only supplies a proxy (not the real object). You should use the .get method to get what you want.

_________________
Don't forget to rate my post if it helped. :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.