-->
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: Problems with Session.get ( Class clazz, Serializable id )
PostPosted: Wed Feb 11, 2004 9:30 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
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 ?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 10:04 am 
Beginner
Beginner

Joined: Mon Feb 09, 2004 3:15 pm
Posts: 34
Passing in obj as your Serializable id may have something to do with it :)


System.out.println("isPersistent => " + isPersistent(CommsIn.class,(Serializable) obj));


Top
 Profile  
 
 Post subject:
PostPosted: Wed Feb 11, 2004 10:09 am 
Beginner
Beginner

Joined: Thu Jan 22, 2004 6:16 am
Posts: 40
Location: Luxembourg
You are right, now it works :=)

The change to do is to write :

sess.get(CommsIn.class, sess.getIdentifier(obj)

Thanks a lot !


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.