Link to home
Start Free TrialLog in
Avatar of mmoore
mmooreFlag for United States of America

asked on

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
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mmoore

ASKER

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
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
Avatar of mmoore

ASKER

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.
:)