You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by Islay Symonette <is...@hotmail.com> on 2005/10/26 19:41:49 UTC
Storing Java Objects in a table
Hello,
Sorry to bother you with such a trivial question.
I am trying to create a table with one of the columns storing a java object.
The sql statement I have is:
CREATE TABLE StoreObject (objectID INT, object MyJavaObject);
And recieved this:
ERROR 42X01: Syntax error: Encountered "" at line 1, column 46.
I understand from the manuals that inorder to load a java class, you have to
store it in a jar file.
I created a jar containing the class for the object using eclipse. Location:
C:\Derby\testingjar.jar
Then using ij I typed (after connecting to the database):
CALL SQLJ.install_jar('c:\derby\testingjar.jar', 'APP.TestLogic', 0);
0 rows inserted/updated/deleted
CALL SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.database.classpath',
'APP.TestLogic');
0 rows inserted/updated/deleted
I'm afraid I don't know where I've gone wrong, any help would be greatly
appreciated.
Thanks,
Islay
_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today it's FREE!
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
Re: Storing Java Objects in a table
Posted by Daniel John Debrunner <dj...@debrunners.com>.
Rick Hillegas wrote:
> I recommend that you make your class implement java.io.Externalizable.
> That way you can write your own deserialization logic to handle version
> upgrades of your class. Don't rely on java.io.Serializable; it doesn't
> handle versioning and isn't appropriate for persisting data; as soon as
> you make a change to your class, you won't be able to deserialize old
> versions.
I agree with the recommendation to use, or at least consider,
Externalizable, but I think you are a little harsh on Serializable. If
you follow the guidelines you can modify the class and continue to
deserialize old versions.
http://java.sun.com/j2se/1.4.2/docs/guide/serialization/spec/version.html#wp6519
Dan.
Re: Storing Java Objects in a table
Posted by Rick Hillegas <Ri...@Sun.COM>.
Hi Islay,
It is true that old versions of Cloudscape let you declare a column to
be a Serializable type. This functionality was removed from Derby
because the syntax was non-standard. Most of the machinery to support
this useful feature, however, still exists. I have logged enhancement
request 651 to track the re-enabling of this functionality modulo
agreement on some standard syntax.
That doesn't help you right now, though. As a crude workaround, declare
a column of some binary or char type (including blob and clob). Then
serialize your object to a byte array or io stream in your applicaton.
Then write this array or stream to the column yourself using methods on
PreparedStatement like setBytes() or setBinaryStream().
I recommend that you make your class implement java.io.Externalizable.
That way you can write your own deserialization logic to handle version
upgrades of your class. Don't rely on java.io.Serializable; it doesn't
handle versioning and isn't appropriate for persisting data; as soon as
you make a change to your class, you won't be able to deserialize old
versions.
I'm happy to give you more advice if this is too cryptic.
Regards,
-Rick
Islay Symonette wrote:
> Hello,
> Sorry to bother you with such a trivial question.
>
> I am trying to create a table with one of the columns storing a java
> object. The sql statement I have is:
>
> CREATE TABLE StoreObject (objectID INT, object MyJavaObject);
>
> And recieved this:
> ERROR 42X01: Syntax error: Encountered "" at line 1, column 46.
>
> I understand from the manuals that inorder to load a java class, you
> have to store it in a jar file.
> I created a jar containing the class for the object using eclipse.
> Location: C:\Derby\testingjar.jar
> Then using ij I typed (after connecting to the database):
>
> CALL SQLJ.install_jar('c:\derby\testingjar.jar', 'APP.TestLogic', 0);
> 0 rows inserted/updated/deleted
> CALL
> SYSCS_UTIL.SYSCS_SET_DATABASE_PROPERTY('derby.database.classpath',
> 'APP.TestLogic');
> 0 rows inserted/updated/deleted
>
> I'm afraid I don't know where I've gone wrong, any help would be
> greatly appreciated.
> Thanks,
> Islay
>
> _________________________________________________________________
> Express yourself instantly with MSN Messenger! Download today it's
> FREE! http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/
>