|
From: Mark P. <mar...@co...> - 2003-08-06 19:07:11
|
Hi,
Thanks, that is a better way, I was only looking at 0.9.
- Mark
-----Original Message-----
From: j=FCrgen h=F6ller [werk3AT] [mailto:jue...@we...]=20
Sent: Wednesday, August 06, 2003 2:29 AM
To: Mark Pollack; spr...@li...
Subject: Re: [Springframework-developer] Suggestion for a callback
implementation.
Hi Mark,
=20
It's easier to use an inner class and simply access a final objectToSave
parameter of the surrounding method, a la:
=20
public void saveMyObject(final MyObject objectToSave) {
hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws
HibernateException {
session.saveOrUpdate(objectToSave);
return null;
}
});
}
=20
HibernateTemplate already provides convenience methods for such
single-step actions since mid-July (after 0.9, but in CVS for a while),
like:
=20
hibernateTemplate.saveOrUpdate(objectToSave);
hibernateTemplate.find(query)
=20
etc. The goal is to avoid the overhead of having to write a callback
implementation for just a single line of Hibernate persistence code.
This works quite nicely in practice, especially in combination with
HibernateTransactionManager and JtaTransactionManager that make
HibernateTemplate actions reuse the same Session instance within a whole
transaction.
=20
BTW in my DAOs, I still prefer to have strongly-typed methods not only
for find but for load and store too (like for "MyObject" above), for
clearness of the API, and to keep the option of alternative
implementations with plain JDBC. In a Hibernate version of a DAO, such
methods are often implemented as one-liners with HibernateTemplate's
convenience methods.
=20
Regards,
Juergen
=20
=20
-----Urspr=FCngliche Nachricht-----=20
Von: Mark Pollack [mailto:mar...@co...]=20
Gesendet: Mi 06.08.2003 02:34=20
An: spr...@li...=20
Cc:=20
Betreff: [Springframework-developer] Suggestion for a callback
implementation.
=09
=09
Hi,
=09
I was playing around with spring and hibernate and ended up
writing a
HibernateSaveCallback class that would seem to be generally
useful. It
goes something like this, though there are many variations,
maybe to the
point of parameterizing which method gets called on the session,
passing
object arrays in the ctor...etc.
=09
public class HibernateSaveCallback implements HibernateCallback
{
=09
private Object _object;
=09
public HibernateSaveCallback(Object objectToSave) {
_object =3D objectToSave; =20
}
=09
public Object doInHibernate(Session session)
throws HibernateException, RuntimeException {
session.save(_object);
return _object;
}
=09
}
=09
=09
How do people generally handle saving to hibernate using the
HibernateTemplate in this case?
=09
Cheers,
Mark
=09
=09
=09
=09
-------------------------------------------------------
This SF.Net email sponsored by: Free pre-built ASP.NET sites
including
Data Reports, E-commerce, Portals, and Forums are available now.
Download today and enter to win an XBOX or Visual Studio .NET.
=09
http://aspnet.click-url.com/go/psa00100003ave/direct;at.aspnet_072303_01
/01
_______________________________________________
Springframework-developer mailing list
Spr...@li...
=09
https://lists.sourceforge.net/lists/listinfo/springframework-developer
=09
|