I want to check how/if the get-method works fine, but unfortunately it doesn't. My test is this. First save an object to the database, and then check if I can retrieve it. If I can, it means the object is in the database, otherwise not. The CommsIn class implements Serializable and is the class used for the test.
Here is the code :
/**
* persist a transient object to the database
*/
public void create( Object obj ) throws PersistenceException
{
Session sess;
try
{
sess = sessionFactory.openSession();
// Save the object to the database
Transaction t = sess.beginTransaction();
sess.save( obj );
t.commit();
sess.close();
// Call isPersisten to see if it's in the database
System.out.println("isPersistent => " + isPersistent(CommsIn.class,(Serializable) obj));
}
catch ( Throwable e )
{}
/**
* check if an object is already persisted in database
*
* @param clazz a persistent class
* @param id an identifier
*
* @return true if object already persisted, false otherwise
*/
public boolean isPersistent( Class clazz, Serializable id ) throws PersistenceException
{
Object obj = null;
Session sess;
boolean isPersisted = false ;
try
{
// Check if the object is persisted
sess = sessionFactory.openSession( );
obj = sess.get(clazz, id);
sess.close();
return obj != null ? true : false;
}
catch ( Throwable e )
{
LogUtils.logProblem( logger,"cannot check if object of type " + obj.getClass( ).getName( ) + " is persistent " +" " + e.getMessage);
But I get this Exception :
com.hp.mw.csf.AccessManager - cannot create object of type com.abpro.babel.CommsIn null
java.lang.NullPointerException
when running the line :
obj = sess.get(clazz, id);
in the isPersistent method.
Ideas ?
|