Hibernate get or load not raising HibernateException when record not found

I am trying to do:
        
        public static Tprogram findProgramByKey( Long key) throws DataAccessException
    	{
            Tprogram program = null;
    		try
    		{
    		   program = (Tprogram)HibernateSessionConfig.getCurrentSession().load(Tprogram.class, key);
    		}
    		catch( HibernateException e )
    		{
    			LOGGER.error( "Unable to find program for key = "+ key, e);
    			throw new DataAccessException( e);
    		}
    		return program;
    	}

Open in new window


I am deliberately passing a key that should result in a "not found", or something like that.
When I use get, program=null, when I use load, all program class variables are null.
Using Hibernate-2.1
LVL 3
mmooreAsked:
Who is Participating?
 
CEHJCommented:
I can't find the javadoc for that class but  make sure it DOES throw that exception. The exception is unchecked so your compiler won't tell you if it doesn't
0
 
mmooreAuthor Commented:
The problem is, it does not throw ANY exception.  As a hack I am now doing ...
    		if (program == null) {
    			LOGGER.error( "Unable to find program for key = "+ key);
    			throw new DataAccessException( "Program does not exist. " + key);
            }

Open in new window

and this works, however, I'm guessing there is a better way to do it.
Mike
0
 
CEHJCommented:
The problem is, it does not throw ANY exception.
Well,  i'm wondering why you were trying to catch one then? ;)

I can't speak with authority as i don't know that API, but not finding a datum would seldom be a reason for an exception
0
 
mmooreAuthor Commented:
Well, I found the java doc and basically it says if the record is not found, null will be returned. So it's working just fine. http://docs.jboss.org/hibernate/orm/3.5/javadocs/org/hibernate/Session.html#get(java.lang.Class, java.io.Serializable)

Thanks for pointing me in the right direction. I'm an old programmer, but new to the web technologies.
0
 
CEHJCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.