You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by John Meredith <ps...@t-online.de> on 2003/11/05 10:18:14 UTC
Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for
answers.]
Hi,
This is *exactly* the way I've been handling my Hibernate objects, and
it works wonderfully.
Just my 2c.
- John
On Wed, 2003-11-05 at 09:00, Ryan wrote:
> Hi there
>
> I use JDO Genie to do my persistence. Since I am working with a pure object
> model typically I only have to keep a handle on the root object of the graph
> per page. Just for performance reasons alone it doesnt make sense to leave
> the root instance floating about on the server. I have over come this by
> writing a DataSqueezer that converts my JDO objects to an object id and back
> again. Then, on your page you keep your root graph object in a hidden
> field. Tapestry does all the squeezing and unsqueezing for you thus making
> it appear as though you are dealing with the actual graph all the time. So,
> you get the graph, freshly obtained from cache or query, set back on your
> page and then all the rewinding takes place.
>
> This insures you are always working with the most up to date copy of the
> graph when the form is posted and that server side resources are spared and
> that you get your moneys worth from the JDO cache :-)
>
> My 2p
>
> Cheers
> Ryan Moffat
>
> ----- Original Message -----
> From: "Cherry Development" <av...@cherrydev.com>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Wednesday, November 05, 2003 6:44 AM
> Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for
> answers.]
>
>
> >
> > On Nov 4, 2003, at 9:48 PM, Harish Krishnaswamy wrote:
> >
> > > If you want behavior in your accessors, you will want to create a
> > > synthetic property using property-specification and use this property
> > > within your real accessor method. For ex.
> > >
> > > public abstract PropType getSyntheticProp(); // property defined via
> > > specfication
> > >
> > > public PropType getProp()
> > > {
> > > ...
> > > ... getSyntheticProp() ...
> > > ...
> > > }
> > >
> >
> > I'm trying to think of a reason why this is necessary. Why shouldn't
> > the enhanced subclass simply call the base class's accessors if they
> > exist and use their default behavior otherwise? Was this a design
> > decision or simply nobody thought of reason why somebody would want
> > this? I assume the problem is that you need two different ways of
> > setting these values, one a 'user' method, allowing for special logic,
> > and one for 'system' access, for use when page properties are being
> > restored from session and need to be set directly. In WebObjects, this
> > is done by allowing for a second set of accessor methods, named the
> > same, but starting with an underscore. I would expect these to only be
> > used when the page was being saved to session and restored from session
> > and the 'normal' accessors used at all other times. I know it's rude
> > for a newbie to suggest architectural changes to a tool only recently
> > picked up, but if I implemented this change, would anybody be opposed
> > to it?
> > >
> > > As far as I know, persistent properties may be serialized because they
> > > are stored in HttpSession and the container may serialize it for
> > > various reasons. Again, I am not sure of OJB, but I have done some
> > > work with JDOs and with JDOs you would detach the entity object from
> > > the persistence manager before passing it to the page and persisting
> > > it there (although I don't like the idea of persisting these objects
> > > in the page). And when you want to persist the entity back in the
> > > database you would just attach it to the persistence manager which
> > > would take care of the rest. Now if this is not possible with OJB, the
> > > only thing I can think of is to make a copy of the entity and store it
> > > in the page. But I have to say I rarely find myself using persistent
> > > properties in my pages. I like to keep my properties in my pages so
> > > there are no synchronization problems between the client state and the
> > > session state. So in pageBeginRender I create a hollow object
> > > structure that will be populated by Tapestry at the end of the rewind,
> > > then I perform my actions in the listeners and set my values in the
> > > object structure and Tapestry would read these values and render the
> > > page. If I want to update the database with the values from the page,
> > > I do so in the listener by querying the object and setting the
> > > appropriate values.
> > >
> >
> > I can see different styles of design being used in Tapestry compared to
> > what I'm used to. WebObjects has very few restrictions on what sort of
> > objects you keep around on your pages because the pages never get
> > pooled. New ones keep getting created and old ones only get removed
> > when they fall off the end of the page cache (the cache is used to
> > handle browser backtracking).
> >
> > If I understand what you're telling me, the ONLY reason my objects
> > might get serialized is if the servlet container serializes the
> > HttpSession. If that's so, why does the documentation say that
> > Tapestry makes copies of all persistent page attributes when it moves
> > them into the HttpSession? The old documentation states that Tapestry
> > explicitly serializes the page state information before storing it in
> > the HttpSession. Is this inaccurate? What, exactly, does TAPESTRY
> > (not the servlet container) do with persistent page attributes before
> > and after a request?
> >
> > Thanks
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
--
John Meredith <ps...@t-online.de>
RE: [Fwd: Re: OJB, Tapestry, Serialized Objects and a
questforanswers.]
Posted by John Meredith <ps...@t-online.de>.
Thanks for the explanation Tsvetelin, cleared things up :-)
Rgds,
- John
On Wed, 2003-11-05 at 16:55, tsvetelin wrote:
> Hi John,
>
> You don't need to encode the class name in your JDO squeezer because this is
> handled by JDO implementations.
> Here are 2 cases that are possible.
>
> The first one is when the JDO implementation define your JDO identity class.
> In this case the JDO implementation use its identity class. This class
> contains the primary keys for your object (in the most cases it is a int or
> long value) and the class name of corresponded data object. When you take
> the string presentation of the identity instance it encode class name. So
> the JDO implementation know persistence class name for each instance of
> identity object. For example I am using the Kodo JDO and their identity
> class is: com.solarmetric.kodo.util.ObjectIds$Id. It contains 2 properties.
> long and persistence class
>
> The second case is when you define the identity class for particular
> persistent class in package.jdo file. For more information please see the
> JDO metadata(<!ATTLIST class identity-type (application|datastore|none)
> 'datastore'> and <!ATTLIST class objectid-class CDATA #IMPLIED> ). In this
> case the JDO implementation know which is associated persistent class for
> particular identity object.
>
>
> I hope that this will be useful.
>
> Tsvetelin
>
> -----Original Message-----
> From: John Meredith [mailto:psynix@t-online.de]
> Sent: Wednesday, November 05, 2003 4:48 PM
> To: Tapestry users
> Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a
> questforanswers.]
>
>
> Possibly a stupid question (and somewhat OT), do you have an Adaptor for
> each JDO and if not, how do you get from the oid back to the object
> without distinguishing between your ModelObjects? Or does JDO encode
> this into the oid?
>
> I generally use integer sequences for my iod (provided by the DB), which
> is why I've had to encode the classname as well.
>
> Just interested in how other people are doing this :-)
>
> - John
>
> On Wed, 2003-11-05 at 12:23, Ryan Moffat wrote:
> > Yeah that's pretty much how the JDO squeezer is written. My only change
> is
> > that my persitance capable classes all implement a common interface
> (always
> > found this usefull) defining a getId() method which I use in the Squeezer.
> >
> > /**
> > * Squeezes the {@link is.symbian.framework.model.ModelObject} data
> to
> > it's oid string
> > */
> > public String squeeze(DataSqueezer squeezer, Object data) {
> > is.symbian.framework.model.ModelObject jdoObject =
> > (is.symbian.framework.model.ModelObject) data;
> > return PREFIX + jdoObject.getObjectId().toString();
> > }
> >
> > /**
> > * Un-squeezes a jdo object using it's oid string to locate it.
> > */
> > public Object unsqueeze(DataSqueezer squeezer, String string) {
> > PersistenceManager pm = JDOSupport.getPM();
> > if (string.length() == PREFIX.length()) return ""; // Probably a
> > problem
> > Object o = pm.newObjectIdInstance(null,
> > string.substring(PREFIX.length()));
> > return pm.getObjectById(o, true);
> > }
> >
> > Cheers
> > Ryan Moffat
> >
> > ----- Original Message -----
> > From: "John Meredith" <ps...@t-online.de>
> > To: "Tapestry users" <ta...@jakarta.apache.org>
> > Sent: Wednesday, November 05, 2003 10:29 AM
> > Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
> > foranswers.]
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> --
> John Meredith <ps...@t-online.de>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
--
John Meredith <ps...@t-online.de>
RE: [Fwd: Re: OJB, Tapestry, Serialized Objects and a questforanswers.]
Posted by tsvetelin <ts...@rushmore-digital.com>.
Hi John,
You don't need to encode the class name in your JDO squeezer because this is
handled by JDO implementations.
Here are 2 cases that are possible.
The first one is when the JDO implementation define your JDO identity class.
In this case the JDO implementation use its identity class. This class
contains the primary keys for your object (in the most cases it is a int or
long value) and the class name of corresponded data object. When you take
the string presentation of the identity instance it encode class name. So
the JDO implementation know persistence class name for each instance of
identity object. For example I am using the Kodo JDO and their identity
class is: com.solarmetric.kodo.util.ObjectIds$Id. It contains 2 properties.
long and persistence class
The second case is when you define the identity class for particular
persistent class in package.jdo file. For more information please see the
JDO metadata(<!ATTLIST class identity-type (application|datastore|none)
'datastore'> and <!ATTLIST class objectid-class CDATA #IMPLIED> ). In this
case the JDO implementation know which is associated persistent class for
particular identity object.
I hope that this will be useful.
Tsvetelin
-----Original Message-----
From: John Meredith [mailto:psynix@t-online.de]
Sent: Wednesday, November 05, 2003 4:48 PM
To: Tapestry users
Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a
questforanswers.]
Possibly a stupid question (and somewhat OT), do you have an Adaptor for
each JDO and if not, how do you get from the oid back to the object
without distinguishing between your ModelObjects? Or does JDO encode
this into the oid?
I generally use integer sequences for my iod (provided by the DB), which
is why I've had to encode the classname as well.
Just interested in how other people are doing this :-)
- John
On Wed, 2003-11-05 at 12:23, Ryan Moffat wrote:
> Yeah that's pretty much how the JDO squeezer is written. My only change
is
> that my persitance capable classes all implement a common interface
(always
> found this usefull) defining a getId() method which I use in the Squeezer.
>
> /**
> * Squeezes the {@link is.symbian.framework.model.ModelObject} data
to
> it's oid string
> */
> public String squeeze(DataSqueezer squeezer, Object data) {
> is.symbian.framework.model.ModelObject jdoObject =
> (is.symbian.framework.model.ModelObject) data;
> return PREFIX + jdoObject.getObjectId().toString();
> }
>
> /**
> * Un-squeezes a jdo object using it's oid string to locate it.
> */
> public Object unsqueeze(DataSqueezer squeezer, String string) {
> PersistenceManager pm = JDOSupport.getPM();
> if (string.length() == PREFIX.length()) return ""; // Probably a
> problem
> Object o = pm.newObjectIdInstance(null,
> string.substring(PREFIX.length()));
> return pm.getObjectById(o, true);
> }
>
> Cheers
> Ryan Moffat
>
> ----- Original Message -----
> From: "John Meredith" <ps...@t-online.de>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Wednesday, November 05, 2003 10:29 AM
> Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
> foranswers.]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
--
John Meredith <ps...@t-online.de>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
foranswers.]
Posted by John Meredith <ps...@t-online.de>.
Possibly a stupid question (and somewhat OT), do you have an Adaptor for
each JDO and if not, how do you get from the oid back to the object
without distinguishing between your ModelObjects? Or does JDO encode
this into the oid?
I generally use integer sequences for my iod (provided by the DB), which
is why I've had to encode the classname as well.
Just interested in how other people are doing this :-)
- John
On Wed, 2003-11-05 at 12:23, Ryan Moffat wrote:
> Yeah that's pretty much how the JDO squeezer is written. My only change is
> that my persitance capable classes all implement a common interface (always
> found this usefull) defining a getId() method which I use in the Squeezer.
>
> /**
> * Squeezes the {@link is.symbian.framework.model.ModelObject} data to
> it's oid string
> */
> public String squeeze(DataSqueezer squeezer, Object data) {
> is.symbian.framework.model.ModelObject jdoObject =
> (is.symbian.framework.model.ModelObject) data;
> return PREFIX + jdoObject.getObjectId().toString();
> }
>
> /**
> * Un-squeezes a jdo object using it's oid string to locate it.
> */
> public Object unsqueeze(DataSqueezer squeezer, String string) {
> PersistenceManager pm = JDOSupport.getPM();
> if (string.length() == PREFIX.length()) return ""; // Probably a
> problem
> Object o = pm.newObjectIdInstance(null,
> string.substring(PREFIX.length()));
> return pm.getObjectById(o, true);
> }
>
> Cheers
> Ryan Moffat
>
> ----- Original Message -----
> From: "John Meredith" <ps...@t-online.de>
> To: "Tapestry users" <ta...@jakarta.apache.org>
> Sent: Wednesday, November 05, 2003 10:29 AM
> Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
> foranswers.]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
--
John Meredith <ps...@t-online.de>
Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest foranswers.]
Posted by Ryan Moffat <ry...@hotmail.com>.
Yeah that's pretty much how the JDO squeezer is written. My only change is
that my persitance capable classes all implement a common interface (always
found this usefull) defining a getId() method which I use in the Squeezer.
/**
* Squeezes the {@link is.symbian.framework.model.ModelObject} data to
it's oid string
*/
public String squeeze(DataSqueezer squeezer, Object data) {
is.symbian.framework.model.ModelObject jdoObject =
(is.symbian.framework.model.ModelObject) data;
return PREFIX + jdoObject.getObjectId().toString();
}
/**
* Un-squeezes a jdo object using it's oid string to locate it.
*/
public Object unsqueeze(DataSqueezer squeezer, String string) {
PersistenceManager pm = JDOSupport.getPM();
if (string.length() == PREFIX.length()) return ""; // Probably a
problem
Object o = pm.newObjectIdInstance(null,
string.substring(PREFIX.length()));
return pm.getObjectById(o, true);
}
Cheers
Ryan Moffat
----- Original Message -----
From: "John Meredith" <ps...@t-online.de>
To: "Tapestry users" <ta...@jakarta.apache.org>
Sent: Wednesday, November 05, 2003 10:29 AM
Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
foranswers.]
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
RE: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for answers.]
Posted by James Iry <ja...@hotmail.com>.
Put the optimistic lock "version" or "timestamp" into a hidden field on your
form after the squeezed object field. On form submit, the object is
reloaded because of the squeeze/unsqueeze protocol. Then the
version/timestamp is written into the object. When you actually try to
save, your persistence tool should check the version/timestamp and throw an
optimistic lock exception if the version/timestamp submitted from the web
doesn't match the one in the database. Just be sure to catch and handle
that exception in your form submission code.
-----Original Message-----
From: Glen Stampoultzis [mailto:gstamp@iinet.net.au]
Sent: Sunday, November 09, 2003 3:30 AM
To: Tapestry users
Subject: Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for
answers.]
Sorry for the rather late reply.
Just a couple of questions:
1. It seems as though this will not work for composite keys or keys that
are not ints. Is that right?
2. If you reload the object don't you loose optimistic locking?
To explain what I mean with point 2 an example:
1. Web page loads hibernate object with up-to-date data from the database
and displays the results to the screen.
2. External changes to the record representing that object occur. 3. User
edits fields and submits form. 4. Hibernate object is squeezed and
unsqueezed causing a reload of the
object from the db.
5. Tapestry writes the modified fields into the object.
Now because the object is being reloaded (in 4) doesn't that mean hibernate
can't detect that there's been a modification to the record?
I could easily be wrong as I'm new to hibernate and tapestry.
Regards,
Glen
At 09:29 PM 5/11/2003, you wrote:
>Sure (see below).
>
>I played around with the classname, because I couldn't think of a
>better way of distinguishing the different Hibernate objects. I'd love
>to know if there's a more appropriate way to to that.
>
>To tell Tapestry about your shiny new Adaptor, you'll also need to
>override createDataSqueezer() in your Engine, something like:
>
>public DataSqueezer createDataSqueezer() {
> return new DataSqueezer(getResourceResolver(),
> new ISqueezeAdaptor[] {new HibernateObjectAdaptor()});
>}
>
>HibernateObject is just an interface defining getId() method and is
>implemented by all my Hibernate objects.
>
>Not sure how whether this will come out wrapped and looking shocking,
>but I hope this helps. Suggestions/Improvements welcome.
>
> - John
>
>----------------------------------
>import java.util.StringTokenizer;
>
>import net.sf.hibernate.HibernateException;
>import net.sf.hibernate.Session;
>
>import org.apache.commons.logging.Log;
>import org.apache.commons.logging.LogFactory;
>import org.apache.tapestry.ApplicationRuntimeException;
>import org.apache.tapestry.util.io.DataSqueezer;
>import org.apache.tapestry.util.io.ISqueezeAdaptor;
>
>import de.tuerkas.resin.Persistence; // Open session in view import
>de.tuerkas.tmarket.model.HibernateObject;
>
>/**
> * Squeezes a HibernateObject
> *
> * @author John Meredith
> * @version $Id: HibernateObjectAdaptor.java,v 1.1 2003/11/03
>18:06:56 johnm Exp $
> *
> **/
>public class HibernateObjectAdaptor implements ISqueezeAdaptor {
> private static Log log =
>LogFactory.getLog(HibernateObjectAdaptor.class);
>
> // TODO: Investigate why PREFIX must be one character.
> private static final String PREFIX = "H";
>
> private static final String BASE_PACKAGE =
> "de.tuerkas.tmarket.model";
>
> public void register(DataSqueezer squeezer) {
> squeezer.register(PREFIX, HibernateObject.class, this);
> }
>
> public String squeeze(DataSqueezer squeezer, Object data) {
> HibernateObject ho = (HibernateObject) data;
>
> String fullClassname = ho.getClass().getName();
> int dotx = fullClassname.indexOf(".",
BASE_PACKAGE.length());
> String className = fullClassname.substring(dotx + 1);
>
> String result = PREFIX + "|" + className + "|" +
>ho.getId().toString();
>
> if (log.isDebugEnabled())
> log.debug("Squeezed: " + result);
>
> return result;
> }
>
> public Object unsqueeze(DataSqueezer squeezer, String string) {
> if (log.isDebugEnabled())
> log.debug("Unsqueezing: " + string);
>
> StringTokenizer st = new StringTokenizer(string, "|");
> st.nextElement();
> String objClassName = BASE_PACKAGE + "." + (String)
> st.nextElement();
> Long objId = new Long((String) st.nextElement());
>
> Session sess = null;
> HibernateObject result = null;
> try {
> sess = Persistence.getSession();
> result = (HibernateObject)
> sess.load(Class.forName(objClassName),
>objId);
> } catch (ClassNotFoundException cnfe) {
> log.error("Cannot find class '" + objClassName +
> "' for id value " +
>objId);
> throw new ApplicationRuntimeException("Unable to
> find class", cnfe);
> } catch (HibernateException he) {
> // TODO: Could be handled better
> throw new ApplicationRuntimeException("Problems
> with database
>access", he);
> }
>
> return result;
> }
>}
>
>On Wed, 2003-11-05 at 10:48, Glen Stampoultzis wrote:
> > Any chance of seeing some sample code snippets?
> >
> > Regards,
> >
> > Glen
> >
> > At 08:18 PM 5/11/2003, you wrote:
> > >Hi,
> > >
> > >This is *exactly* the way I've been handling my Hibernate objects,
> > >and it works wonderfully.
> > >
> > >Just my 2c.
> > >
> > > - John
> >
> >
> > Glen Stampoultzis
> > gstamp@iinet.net.au http://members.iinet.net.au/~gstamp/glen/
>--
>John Meredith <ps...@t-online.de>
Glen Stampoultzis
gstamp@iinet.net.au
http://members.iinet.net.au/~gstamp/glen/
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
for answers.]
Posted by Glen Stampoultzis <gs...@iinet.net.au>.
Sorry for the rather late reply.
Just a couple of questions:
1. It seems as though this will not work for composite keys or keys that
are not ints. Is that right?
2. If you reload the object don't you loose optimistic locking?
To explain what I mean with point 2 an example:
1. Web page loads hibernate object with up-to-date data from the database
and displays the results to the screen.
2. External changes to the record representing that object occur.
3. User edits fields and submits form.
4. Hibernate object is squeezed and unsqueezed causing a reload of the
object from the db.
5. Tapestry writes the modified fields into the object.
Now because the object is being reloaded (in 4) doesn't that mean hibernate
can't detect that there's been a modification to the record?
I could easily be wrong as I'm new to hibernate and tapestry.
Regards,
Glen
At 09:29 PM 5/11/2003, you wrote:
>Sure (see below).
>
>I played around with the classname, because I couldn't think of a better
>way of distinguishing the different Hibernate objects. I'd love to know
>if there's a more appropriate way to to that.
>
>To tell Tapestry about your shiny new Adaptor, you'll also need to
>override createDataSqueezer() in your Engine, something like:
>
>public DataSqueezer createDataSqueezer() {
> return new DataSqueezer(getResourceResolver(),
> new ISqueezeAdaptor[] {new HibernateObjectAdaptor()});
>}
>
>HibernateObject is just an interface defining getId() method and is
>implemented by all my Hibernate objects.
>
>Not sure how whether this will come out wrapped and looking shocking,
>but I hope this helps. Suggestions/Improvements welcome.
>
> - John
>
>----------------------------------
>import java.util.StringTokenizer;
>
>import net.sf.hibernate.HibernateException;
>import net.sf.hibernate.Session;
>
>import org.apache.commons.logging.Log;
>import org.apache.commons.logging.LogFactory;
>import org.apache.tapestry.ApplicationRuntimeException;
>import org.apache.tapestry.util.io.DataSqueezer;
>import org.apache.tapestry.util.io.ISqueezeAdaptor;
>
>import de.tuerkas.resin.Persistence; // Open session in view
>import de.tuerkas.tmarket.model.HibernateObject;
>
>/**
> * Squeezes a HibernateObject
> *
> * @author John Meredith
> * @version $Id: HibernateObjectAdaptor.java,v 1.1 2003/11/03 18:06:56
>johnm Exp $
> *
> **/
>public class HibernateObjectAdaptor implements ISqueezeAdaptor {
> private static Log log =
>LogFactory.getLog(HibernateObjectAdaptor.class);
>
> // TODO: Investigate why PREFIX must be one character.
> private static final String PREFIX = "H";
>
> private static final String BASE_PACKAGE =
> "de.tuerkas.tmarket.model";
>
> public void register(DataSqueezer squeezer) {
> squeezer.register(PREFIX, HibernateObject.class, this);
> }
>
> public String squeeze(DataSqueezer squeezer, Object data) {
> HibernateObject ho = (HibernateObject) data;
>
> String fullClassname = ho.getClass().getName();
> int dotx = fullClassname.indexOf(".", BASE_PACKAGE.length());
> String className = fullClassname.substring(dotx + 1);
>
> String result = PREFIX + "|" + className + "|" +
>ho.getId().toString();
>
> if (log.isDebugEnabled())
> log.debug("Squeezed: " + result);
>
> return result;
> }
>
> public Object unsqueeze(DataSqueezer squeezer, String string) {
> if (log.isDebugEnabled())
> log.debug("Unsqueezing: " + string);
>
> StringTokenizer st = new StringTokenizer(string, "|");
> st.nextElement();
> String objClassName = BASE_PACKAGE + "." + (String)
> st.nextElement();
> Long objId = new Long((String) st.nextElement());
>
> Session sess = null;
> HibernateObject result = null;
> try {
> sess = Persistence.getSession();
> result = (HibernateObject)
> sess.load(Class.forName(objClassName),
>objId);
> } catch (ClassNotFoundException cnfe) {
> log.error("Cannot find class '" + objClassName +
> "' for id value " +
>objId);
> throw new ApplicationRuntimeException("Unable to
> find class", cnfe);
> } catch (HibernateException he) {
> // TODO: Could be handled better
> throw new ApplicationRuntimeException("Problems
> with database
>access", he);
> }
>
> return result;
> }
>}
>
>On Wed, 2003-11-05 at 10:48, Glen Stampoultzis wrote:
> > Any chance of seeing some sample code snippets?
> >
> > Regards,
> >
> > Glen
> >
> > At 08:18 PM 5/11/2003, you wrote:
> > >Hi,
> > >
> > >This is *exactly* the way I've been handling my Hibernate objects, and
> > >it works wonderfully.
> > >
> > >Just my 2c.
> > >
> > > - John
> >
> >
> > Glen Stampoultzis
> > gstamp@iinet.net.au
> > http://members.iinet.net.au/~gstamp/glen/
>--
>John Meredith <ps...@t-online.de>
Glen Stampoultzis
gstamp@iinet.net.au
http://members.iinet.net.au/~gstamp/glen/
Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest for
answers.]
Posted by John Meredith <ps...@t-online.de>.
Sure (see below).
I played around with the classname, because I couldn't think of a better
way of distinguishing the different Hibernate objects. I'd love to know
if there's a more appropriate way to to that.
To tell Tapestry about your shiny new Adaptor, you'll also need to
override createDataSqueezer() in your Engine, something like:
public DataSqueezer createDataSqueezer() {
return new DataSqueezer(getResourceResolver(),
new ISqueezeAdaptor[] {new HibernateObjectAdaptor()});
}
HibernateObject is just an interface defining getId() method and is
implemented by all my Hibernate objects.
Not sure how whether this will come out wrapped and looking shocking,
but I hope this helps. Suggestions/Improvements welcome.
- John
----------------------------------
import java.util.StringTokenizer;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.ApplicationRuntimeException;
import org.apache.tapestry.util.io.DataSqueezer;
import org.apache.tapestry.util.io.ISqueezeAdaptor;
import de.tuerkas.resin.Persistence; // Open session in view
import de.tuerkas.tmarket.model.HibernateObject;
/**
* Squeezes a HibernateObject
*
* @author John Meredith
* @version $Id: HibernateObjectAdaptor.java,v 1.1 2003/11/03 18:06:56
johnm Exp $
*
**/
public class HibernateObjectAdaptor implements ISqueezeAdaptor {
private static Log log =
LogFactory.getLog(HibernateObjectAdaptor.class);
// TODO: Investigate why PREFIX must be one character.
private static final String PREFIX = "H";
private static final String BASE_PACKAGE = "de.tuerkas.tmarket.model";
public void register(DataSqueezer squeezer) {
squeezer.register(PREFIX, HibernateObject.class, this);
}
public String squeeze(DataSqueezer squeezer, Object data) {
HibernateObject ho = (HibernateObject) data;
String fullClassname = ho.getClass().getName();
int dotx = fullClassname.indexOf(".", BASE_PACKAGE.length());
String className = fullClassname.substring(dotx + 1);
String result = PREFIX + "|" + className + "|" +
ho.getId().toString();
if (log.isDebugEnabled())
log.debug("Squeezed: " + result);
return result;
}
public Object unsqueeze(DataSqueezer squeezer, String string) {
if (log.isDebugEnabled())
log.debug("Unsqueezing: " + string);
StringTokenizer st = new StringTokenizer(string, "|");
st.nextElement();
String objClassName = BASE_PACKAGE + "." + (String) st.nextElement();
Long objId = new Long((String) st.nextElement());
Session sess = null;
HibernateObject result = null;
try {
sess = Persistence.getSession();
result = (HibernateObject) sess.load(Class.forName(objClassName),
objId);
} catch (ClassNotFoundException cnfe) {
log.error("Cannot find class '" + objClassName + "' for id value " +
objId);
throw new ApplicationRuntimeException("Unable to find class", cnfe);
} catch (HibernateException he) {
// TODO: Could be handled better
throw new ApplicationRuntimeException("Problems with database
access", he);
}
return result;
}
}
On Wed, 2003-11-05 at 10:48, Glen Stampoultzis wrote:
> Any chance of seeing some sample code snippets?
>
> Regards,
>
> Glen
>
> At 08:18 PM 5/11/2003, you wrote:
> >Hi,
> >
> >This is *exactly* the way I've been handling my Hibernate objects, and
> >it works wonderfully.
> >
> >Just my 2c.
> >
> > - John
>
>
> Glen Stampoultzis
> gstamp@iinet.net.au
> http://members.iinet.net.au/~gstamp/glen/
--
John Meredith <ps...@t-online.de>
Re: [Fwd: Re: OJB, Tapestry, Serialized Objects and a quest
for answers.]
Posted by Glen Stampoultzis <gs...@iinet.net.au>.
Any chance of seeing some sample code snippets?
Regards,
Glen
At 08:18 PM 5/11/2003, you wrote:
>Hi,
>
>This is *exactly* the way I've been handling my Hibernate objects, and
>it works wonderfully.
>
>Just my 2c.
>
> - John
Glen Stampoultzis
gstamp@iinet.net.au
http://members.iinet.net.au/~gstamp/glen/