-->
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.  [ 1 post ] 
Author Message
 Post subject: Please help me in persisting the object in database.
PostPosted: Wed May 18, 2005 1:21 pm 
Newbie

Joined: Wed May 18, 2005 12:58 pm
Posts: 1
Hi,
Actually I am using Hibernate to persist userProfile object. The problem is that it is not persisting the object in the database.
I am calling saveOrUpdate to save the object in the database. Then I call loadById to load the object. It loads the object correctly but I cannot see the object in the database. It is surprising for me how loadBy Id is loading the object when it is not saved in the database. My hunch is that the hibernate is providing the object from its cache.

I have posted the code below. If you scroll down the page and look at how "executeAsUnitOfWork" is written, I do not see any reason why the object should not be persisted.

It does not generate any erros but when I try to access the object through application, it does not find the userProfile object. Also when I query the database, it does not show the object.

Please suggest me what shoudl I do to persist the object. I am opening the session, starting the transaction, persisting object, calling tx.Commit() and then closing the session ( as can be seen from executeAsUnitOfWork). I am also calling session.Flush(). I have also tried various flush options such as COMMIT, ALWAYS, ETC but none is working.

Following is the code:




userProfileEjb.saveOrUpdate(userProfile, userOrganizationList, userGroupList);

The above method in turn calls the following method:

public void saveOrUpdate(UserProfile userProfile, String[] organizationList, String[] userGroupList) throws Exception {

Class[] parameterTypes = new Class[3];
parameterTypes[0] = UserProfile.class;
parameterTypes[1] = String[].class;
parameterTypes[2] = String[].class;

Object[] parameters = new Object[3];
parameters[0] = userProfile;
parameters[1] = organizationList;
parameters[2] = userGroupList;
HibernateBaseDAO.executeAsUnitOfWork(this, "saveOrUpdate", parameterTypes, parameters);

}


/**
* This method saves the persistence Object (UserProfile), related organization and user role associations into the databse.
*
* @param session Hibernate Session used to execute any/all hibernate queries as a unit-of-work
* @param userProfile userProfile persistence object
* @param organizationList List of Organization Ids associated to user
* @param userGroupList List of userGroup Ids associated to user
*/

public void saveOrUpdate(Session session, UserProfile userProfile, String[] organizationList, String[] userGroupList)
throws StaleObjectStateException, Exception {
try {
log.debug("UserProfile called");
int size;
if (userGroupList != null) {
size = userGroupList.length;
} else {
size = 0;
}
log.debug("USERPROFILE: +++ " + StringUtils.getToString(userProfile));
log.debug("USER ROLE LIST: +++" + userGroupList);
log.debug("ORGANIZATION LIST: +++" + organizationList);
session.saveOrUpdate(userProfile);
session.flush();
AssociationRemote associationEjb = (AssociationRemote) ServiceLocator.getRemote(EdrsConstants.ASSOCIATION_JNDI_NAME, AssociationHome.class);
if (size > 0) {
associationEjb.associate(session, userProfile, UserGroup.class, UserProfileGroup.class, userGroupList);
}

if (organizationList != null) {
size = organizationList.length;
} else {
size = 0;
}
if (size > 0) {
associationEjb.associate(session, userProfile, OrganizationProxy.class, UserProfileOrganization.class, organizationList);
}
} catch (Exception e) {
log.error("ERROR: Exception thrown:" + e.getMessage());
e.printStackTrace();
throw e;
}
}



public static Object executeAsUnitOfWork( Object obj,
String callbackMethodName,
Class[] paramTypes,
Object[] parameters )
throws Exception {
Class clazz;
Method myMethod;
Session session = null;
Transaction tx = null;
List findList = new ArrayList();
Class[] newParameterTypes = new Class[paramTypes.length + 1];
Object[] newParameters = new Object[parameters.length + 1];
Object results = null;
try {
session = HibernateUtils.currentSession( false );
tx = session.beginTransaction();
newParameterTypes[0] = Session.class;
newParameters[0] = session;
System.arraycopy( paramTypes,0,newParameterTypes,1,paramTypes.length );
System.arraycopy(parameters,0,newParameters,1,parameters.length );
if (obj instanceof Class) {
clazz = (Class)obj;
} else {
clazz = obj.getClass();
}
myMethod = clazz.getMethod(callbackMethodName, newParameterTypes);
results = myMethod.invoke(obj, newParameters);
tx.commit();
} catch (Exception e) {
log.error("Exception caught:\n" + e.getMessage());
e.printStackTrace();
if ( tx != null ) { tx.rollback(); }
throw e;
} finally {
HibernateUtils.closeSession();
}// try-catch-finally

return results;
}// executeAsUnitOfWork()

[code][/code]


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

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.