users@glassfish.java.net

Re: How does the TopLink (present on GF) persist _at_Serializable Objects?

From: Paulo Cesar Reis <casmeiron_at_gmail.com>
Date: Mon, 04 May 2009 15:36:46 -0300

Sorry, ignore the first lines about abstract class, I was talking about
other entity.

And also I forgot to set the values to the entity:
List<String> values = new ArrayList<String>();
values.add("a");
values.add("b");
MyEntityExample example = new MyEntityExample ();
example.setId(new Long(1));
example.setObject(values);
manager.persist(example);



On 5/4/09 2:46 PM, "Paulo Cesar Silva Reis" <casmeiron_at_gmail.com> wrote:

> Sure, follow:
>
> // Abstract class that is used by 2 child classes. The field that I am
> talking about is Object object.
>
> // Note that it works perfectly when persisted with EntityManager but when I
> try to insert this object writing the bytes directly in the column (using
> insert statement) toplink is not able to reconstruct the object later.
>
> My entity class is simple like this:
>
> @Entity
> public class MyEntityExample implements Serializable {
> private Long id;
>
> private Serializable object;
>
> @Id
> public Long getId( ) {
>
> }
>
> public void setId( Long id ) {
> this.setId(id);
> }
>
> public Serializable getObject( ) { return this.object; }
> public void setObject(Serializable object) { this.object = object; }
>
> So if I do that;
>
> List<String> values = new ArrayList<String>();
> values.add("a");
> values.add("b");
> MyEntityExample example = new MyEntityExample ();
> example.setId(new Long(1));
> manager.persist(example);
>
> Everything work, I can easily get the object using:
>
> List<String> values2 =
> (List<String>) manager.find(MyEntityExample.class, new
> Long(1)).getObject();
> values.equals(values2); // true
>
> But whether a try this approach:
> CallableStatement cs = connection.prepareCall("INSERT INTO MyEntityExamle
> (ID, OBJECT) VALUES(?, ?)");
> cs.sestLong(1, new Long(1));
> List<String> values = new ArrayList<String>();
> values.add("a");
> values.add("b");
>
> ByteArrayOutputStream out = new ByteArrayOutputStream();
>
> ObjectOutputStream objOut = new ObjectOutputStream(out);
>
> objOut.writeObject( values );
> InputStream stream = new ByteArrayInputStream(out.toByteArray());
>
> cs.setBinaryStream(11, stream, out.toByteArray().length);
>
>
> int res = cs.executeUpdate();
> cs.close();
>
> And later whether I try to load the above object using EntityManager using:
>
> List<String> values2 =
> (List<String>) manager.find(MyEntityExample.class, new
> Long(1)).getObject();
>
> I get de described error.
>
> So which way do I need to write the serializable object so TopLink can
> reconstruct it?
>
> Thanks.
>
>
>
> On 5/4/09 11:58 AM, "glassfish_at_javadesktop.org" <glassfish_at_javadesktop.org>
> wrote:
>
>> Can you post your entity class?
>> [Message sent by forum member 'e3133d3' (e3133d3)]
>>
>> http://forums.java.net/jive/thread.jspa?messageID=344852
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>
>