db-jdo-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andy Jefferson <a...@jpox.org>
Subject Re: serialize Detachable instance for storage
Date Sat, 25 Mar 2006 07:27:28 GMT
Hi Craig,

> By the way, Andy, if you have a test case that you can point me to (I  
> hope it's in the repository) we can add it to the TCK or at least  
> implement the ideas contained therein.

A simple testcase that would have failed when we implemented it in a strict JDO2
spec way (always detaching a serialised object) is stored at
JPOX/Core/src/test/org/jpox/persistence/SerializationTest.java (testSerialisedPC)

To give you the code snippet it's very similar to Matthews example earlier except
we're also updating a field in the serialised object

            // Persist an object with a serialised PC field
            try
            {
                tx.begin();

                SerialisedHolder holder = new SerialisedHolder("Holder(1)", new
SerialisedObject("My Description(1)"));
                pm.makePersistent(holder);

                // Update holder and serialised object fields to check that they
get to the datastore
                holder.setName("Holder(2)");
                holder.getSerialisedPC().setDescription("My Description(2)");

                tx.commit();
                holderId = pm.getObjectId(holder);
            }
            catch (Exception e)
            {
                e.printStackTrace();
                LOG.error(e);
                fail("Exception thrown while persisted object with serialised PC
field : " + e.getMessage());
            }
            finally
            {
                if (tx.isActive())
                {
                    tx.rollback();
                }
                pm.close();
            }

            // Retrieve the object and check the contents
            pm = pmf.getPersistenceManager();
            tx = pm.currentTransaction();
            try
            {
                tx.begin();

                SerialisedHolder holder =
(SerialisedHolder)pm.getObjectById(holderId);
                assertTrue("Holder of serialised PC could not be retrieved!",
holder != null);
                assertTrue("Holder name is incorrect",
holder.getName().equals("Holder(2)"));
                assertTrue("Retrieved holder has null serialised object!",
holder.getSerialisedPC() != null);
                assertEquals("Retrieved serialised object description is
incorrect :",holder.getSerialisedPC().getDescription(), "My Description(2)");

                // Update holder and serialised object fields to check that they
get to the datastore
                holder.getSerialisedPC().setDescription("My Description(3)");
                holder.setName("Holder(3)");

                tx.commit();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                LOG.error(e);
                fail("Exception thrown while retrieving object with serialised PC
field : " + e.getMessage());
            }
            finally
            {
                if (tx.isActive())
                {
                    tx.rollback();
                }
                pm.close();
            }

The part that would fail would be the assert on the "description" of the
serialised PC since the update would not have happened if it had been detached
when serialising it into the datastore in part 1 (before its update).


--
Andy

Mime
View raw message