-->
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.  [ 6 posts ] 
Author Message
 Post subject: Help me use session.merge() properly.
PostPosted: Wed Nov 09, 2005 4:03 pm 
Newbie

Joined: Wed Nov 09, 2005 3:55 pm
Posts: 7
I'm using Hibernate 3.

I have to retrieve a persistence instance and update it. When i try retrieving the same record and update it again, it throws an exception "NonUniqueObject"

Then I tried using a session.merge(). it updates the record, but I have a problem retrieving the same record again. it retreives the previous value before the update. however if i refresh the page I get the updated value. (but this is also inconsistent...sometimes i get the updated value while retrieving for the first time and get the wrong values later on)
Sometimes I even get deadlocks while updating the record.

I'm providing here the codes for updating the records.


public static boolean updateDept( Department dept, DepartmentPK pk )
{

try
{
HibernateUtil.beginTransaction( Constants.UDB_DATABASE );
Session session = HibernateUtil.getSession( Constants.UDB_DATABASE );

Department deptInSession = (Department) session.get( Department.class, pk );

copyProperties(dept, deptInSession); // this uses the BeanUtils to copy the properties from dept to deptInSession.
session.merge( deptInSession);

HibernateUtil.commitTransaction( );

session.refresh( dept );
return true;

} catch( HibernateException e )
{

HibernateUtil.rollbackTransaction( );
paslog.error( e.getMessage( ) );
return false;

}

}


pls suggest me on what I can do.
Thanx in advance.
Anand.


Top
 Profile  
 
 Post subject: just merge
PostPosted: Wed Nov 09, 2005 5:07 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Basically merge will do automatically all you try to do manually. As its name suggests it merges state of the given object with a state of persistent object. So, in code it looks like this ( provided that the given department object has proper key):

public static boolean updateDept( Department dept )
{

try
{
HibernateUtil.beginTransaction( Constants.UDB_DATABASE );
Session session = HibernateUtil.getSession( Constants.UDB_DATABASE );

session.merge(dept);

HibernateUtil.commitTransaction( );
return true;

}

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Help me use session.merge() properly.
PostPosted: Thu Nov 10, 2005 12:53 pm 
Newbie

Joined: Wed Nov 09, 2005 3:55 pm
Posts: 7
Hi,
Thanks for the reply.

If I try using the session.merge() alone , it gives a "not-null property references a null or transient value:" exception.
So I explicitly copied the dept properties to the deptInSession properties.

this helped me to resolve the problem and update works fine now. But I get a deadlock when I try to retrieve the rows again for another update.

So I'm not sure whether I'm using the session.merge() properly.
pls reply if u have more suggestions.
Thanks,
Anand.


Top
 Profile  
 
 Post subject: code
PostPosted: Thu Nov 10, 2005 12:55 pm 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
Please post code of that attempt.

_________________
--------------
Konstantin

SourceLabs - dependable OpenSource systems


Top
 Profile  
 
 Post subject: Help me use session.merge() properly.
PostPosted: Thu Nov 10, 2005 4:40 pm 
Newbie

Joined: Wed Nov 09, 2005 3:55 pm
Posts: 7
Hi,



This is the code I use to retrieve records.

public static ArrayList getFluClinicFamily( String enteredBy )
{

ArrayList fluFamilyList = new ArrayList( );
try
{
Session session = HibernateUtil.getSession( Constants.UDB_DATABASE );
StringBuffer sql = new StringBuffer( "from FluClinic as fluClinic " );
sql.append( "where fluClinic.enteredBy = :enteredBy " );
sql.append( "order by fluClinic.enterTimeStamp desc " );

Query query = session.createQuery( sql.toString( ) );
query.setParameter( "enteredBy", enteredBy );

fluFamilyList = (ArrayList) query.list( );

} catch( HibernateException e )
{
paslog.error( e );
return null;
}

return fluFamilyList;

}


I select certain records for update using the following code.

public static ArrayList getFluClinicByTimeStamp( String enterTimeStamp, String enteredBy )
{

ArrayList fluClinicByTimeStampList = new ArrayList( );

try
{

Session session = HibernateUtil.getSession( Constants.UDB_DATABASE );
StringBuffer sql = new StringBuffer( "from FluClinic as fluClinic " );
sql.append( "where fluClinic.enterTimeStamp = :enterTimeStamp " );
sql.append( "and fluClinic.enteredBy = :enteredBy " );
sql.append( "and fluClinic.transactionStatus not in ('D','U','C') " );

Query query = session.createQuery( sql.toString( ) );
query.setParameter( "enterTimeStamp", enterTimeStamp );
query.setParameter( "enteredBy", enteredBy );

fluClinicByTimeStampList = (ArrayList) query.list( );

} catch( HibernateException e )
{
paslog.error( e );
return null;
}

return fluClinicByTimeStampList;

}


And this the actual code I use for updating the records.

public static boolean updateFluClinic( FluClinic fluClinic, FluClinicPK pk )
{

try
{
HibernateUtil.beginTransaction( Constants.UDB_DATABASE );
Session session = HibernateUtil.getSession( Constants.UDB_DATABASE );

FluClinic flu = (FluClinic) session.get( FluClinic.class, pk );

combineProperties(fluClinic, flu);// this copies the fluClinic properties to flu properties.
session.merge( flu);

HibernateUtil.commitTransaction( );

return true;

} catch( HibernateException e )
{

HibernateUtil.rollbackTransaction( );
paslog.error( e.getMessage( ) );
return false;

}

}



I get a deadlock after one round trip. What could be the reasons? this is a part of the application and the Mapping works fine for the main application.

Thanks,
Nithya.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 10, 2005 5:00 pm 
Expert
Expert

Joined: Thu Dec 23, 2004 9:08 pm
Posts: 2008
The problem is that (if it worked) you'd have both fluClinic and flu being persistent objects representing the same row in the database. Hibernate doesn't allow that, but apparently you've found a nifty way around the usual NonUniqueException.

You could go the other way: copy fluClinic properties to flu, because flu is the version you loaded from the database. Then just session.update(flu).

When this sort of thing happened to me, I used session().replicate(fluClinic, ReplicationMode.OVERWRITE). With this, you don't have to re-attach fluClinic, and you don't have to get flu from the DB first. So long as fluClinic's id is correct, it will work.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 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.