db4o Developer Community

db4o open source object database, native to Java and .NET
Welcome to db4o Developer Community Sign in | Join
in Search
More Search Options

Serializable member not stored

Last post 09-05-2007, 09:59 AM by jstammi. 3 replies.
Sort Posts: Previous Next
  •  09-03-2007, 04:09 PM 41211

    Serializable member not stored

    Hi,

    this day I faced up with a problem on storing an object wrapping a java.lang.Serializable member: on restoring the object from the database, the Serializable field is null. The object manager app shows that member as null in the database, too. I tried with db40 releases 6.1 and 6.3, investigated the db with object manager 6.2.

    When defining the member as java.lang.Object it works as expected.

    My stripped down example:

    public class Value {
        private final Serializable fValue;
        public Value(final Serializable value) {
            fValue = value;
        }
        public Serializable get() {
            return (Serializable) fValue;
        }
    }

    I (JUnit-)test the persistence of the above class with the following code snippet:

        public void testSerializableValueStorageInDb4o() throws Exception {
            final Configuration config  = Db4o.newConfiguration();
            config.exceptionsOnNotStorable(true);
            final File testDB
                = File.createTempFile("testSerializableValueStorageInDb4o.",
                                      ".yap",
                                      TEST_OUTPUT_DIR);
            ObjectContainer storage
                = Db4o.openFile(config, testDB.getAbsolutePath());
            final Value val1 = new Value(Integer.valueOf(42));
            storage.set(val1);
            storage.close();
            // restoring from the db4o db
            storage = Db4o.openFile(config, testDB.getAbsolutePath());
            final ObjectSet valQueryResult = storage.query(Value.class);
            assertEquals(1, valQueryResult.size());
            final Object objFromDB = valQueryResult.next();
            assertTrue(objFromDB instanceof Value);
            final Value val1FromDB = (Value) objFromDB;
            assertEquals(Integer.valueOf(42), val1FromDB.get());
            storage.close();
        }

     

    Any ideas on what I'm doing wrong or does it look like a bug?

  •  09-04-2007, 04:02 PM 41259 in reply to 41211

    Re: Serializable member not stored

    I can see two possible causes for your problem.  Firstly, Serializable is an interface and not a concrete class.  If the object you are passing as a parameter into the Value class had previously been stored in the database then your code should work.  Which is to say the variable fValue would contain a non-null reference to the object.  Secondly, the object you are passing is of type Integer and I'm not sure primitive types such as Integer can be stored by themselves.  Normally if you need to store an integer or other primitive type, you would need to store it as part of an Object.  I hope this helps you.

    Regards
    Graham

  •  09-05-2007, 09:53 AM 41298 in reply to 41259

    Re: Serializable member not stored

     > Secondly, the object you are passing is of type Integer and I'm not sure primitive types such as Integer can be stored by themselves.  Normally if you need to store an integer or other primitive type, you would need to store it as part of an Object.  I hope this helps you. 

    The storing of a primitive (and some more meta data) is the purpose of my Value class.

     > Firstly, Serializable is an interface and not a concrete class.  If the object you are passing as a parameter into the Value class had previously been stored in the database then your code should work

    I would have expected at least an ObjectNotStorableException (as I enabled that feature in the configuration) if it is not possible in principle to store a member that is declared by way of an Interface.

    But as I could not believe in the fact, that members that are declared by way of an interface are stored only with having stored the member value separately, I did test this with a self-written test interface: and it worked without any problem as expected (with an "empty" interface as Serializable and with one definiting some behaviour).

    So there needs to be something very special with the Serializable. And with further testing I'm quite sure to know now when this problem occurs: if the member is declared by way of a standard JDK interface and it's value's class is a standard JDK one, too. E.g. storing also fails with declaring with CharSequence and setting it to a String value.

    So I assume this to be a bug ... ?

  •  09-05-2007, 09:59 AM 41299 in reply to 41298

    Re: Serializable member not stored

    ... and I just found it in the issues tracker: http://tracker.db4o.com/browse/COR-712 and http://tracker.db4o.com/browse/COR-722 ...
View as RSS news feed in XML