XMLEncoder not serializing graph

I have this class which is a composite of other classes. One of the composite, Role is itself a composite of class, Recipient. But the XMLEncoder stops at Role and doesn't go any further. All classes are serializable. I am using XMLEncoder in a "regular" way and printing to a file. Should I be doing anything special to output the complete graph?

Thanks in Advance.

Here is the "central" class:

-

public class ServiceNotification implements Serializable

{

private long id;

private List<Role> roles;

private Application application;

private Event event;

private String name;

public ServiceNotification(){}

public ServiceNotification(Application app, Event event, String name)

{

this.application = app;

this.event = event;

this.name = name;

}

@Id

@GeneratedValue

public long getId()

{

return id;

}

public void setId(long id)

{

this.id = id;

}

@OneToOne(fetch = FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})

public Application getApplication()

{

return application;

}

public void setApplication(Application application)

{

this.application = application;

}

@OneToOne(fetch = FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})

public Event getEvent()

{

return event;

}

public void setEvent(Event event)

{

this.event = event;

}

@OneToMany(fetch = FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})

public List<Role> getRoles()

{

return roles;

}

public void setRoles(List<Role> roles)

{

this.roles = roles;

}

--

Role contains Recipients:

public class Role implements Serializable

{

private long id;

private String name;

private List<Recipient> recipients = new ArrayList<Recipient>();

public Role(){}

public Role(long id){this.id = id;}

public Role(String name){this.name = name;}

@Id

@GeneratedValue

public long getId()

{

return id;

}

public void setId(long id)

{

this.id = id;

}

/**

* @return Returns the role_name.

*/

public String getName()

{

return name;

}

/**

* @return Returns the recipients.

*/

@ManyToMany(fetch = FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})

@JoinTable

(

name="ROLE_RECIPIENT",

joinColumns=@JoinColumn(name="ROLE_ID", referencedColumnName="ID"),

inverseJoinColumns=@JoinColumn(name="RECIPIENT_ID", referencedColumnName="ID")

)

public List<Recipient> getRecipients()

{

return recipients;

}

/**

* @param recipients The recipients to set.

*/

public void setRecipients(List<Recipient> recipients)

{

this.recipients = recipients;

}

/**

* @param role_name The role_name to set.

*/

public void setRole_name(String name)

{

this.name = name;

}

public void setName(String name)

{

this.name = name;

}

The XML output is:

<?xml version="1.0" encoding="UTF-8"?>

<java version="1.5.0_06" class="java.beans.XMLDecoder">

<object class="java.util.ArrayList">

<void method="add">

<object id="ServiceNotification0" class="examplen.ServiceNotification">

<void property="application">

<object class="examplen.Application">

<void property="id">

<long>1</long>

</void>

<void property="name">

<string>FLEET_MANAGER</string>

</void>

</object>

</void>

<void property="event">

<object class="examplen.Event">

<void property="action">

<string>TO BE DETERMINED</string>

</void>

<void property="description">

<string>An airplane in the database has been changed</string>

</void>

<void property="id">

<long>1</long>

</void>

<void property="type">

<string>AIRPLANE_CHANGE</string>

</void>

</object>

</void>

<void property="id">

<long>1</long>

</void>

<void property="roles">

<object class="org.hibernate.collection.PersistentBag">

<void property="owner">

<object idref="ServiceNotification0"/>

</void>

</object>

</void>

</object>

</void>

</object>

</java>

-

[5083 byte] By [globetrotcoma] at [2007-11-26 16:45:00]
# 1
Does PersistentBag obey the rules for JavaBeans?
ejpa at 2007-7-8 23:12:19 > top of Java-index,Core,Core APIs...
# 2
"PersistentBag" was put in by XMLEncoder, not by me!As you can see, "roles" is List<Role>, an arraylist! Similarly, recipients of a role are List<Recipient>.Any ideas?Thanks
globetrotcoma at 2007-7-8 23:12:19 > top of Java-index,Core,Core APIs...
# 3

> As you can see, "roles" is List<Role>, an arraylist!

No, I can't see any such thing. List<Role> is not an ArrayList and it isn't initialized to one anywhere in your code. The XML that was produced shows that the List<Role> interface was implemented by a PersistentBag<Role> object at the time of XMLEncoding.

Can you answer the question please?

ejpa at 2007-7-8 23:12:19 > top of Java-index,Core,Core APIs...
# 4

You are right. It is not explicitly shown to be ArrayList. I just assumed, wrongly, that it is.

I am letting EJB3/JPA take care of how the roles and recipients are assigned. In my case JPA is being implemented by Hibernate (JBoss). So it is, probably, Hibernate that is using PersistentBag.

When I changed, in ServiceNotification, from:

private List<Role> roles;

to ...

private List<Role> roles = new ArrayList<Role>();

roles STOPPED showing in XML altogether!

I am running XMLEncoder after I retrieve a ServiceNotification object from the database which does have Roles and Recipients assigned to it.

Given this situation, what can I do to be able to serialize the whole graph using XMLEncoder? I plan to import data into the underlying database, later, using XMLDecode.

Let me know if I can provide you with any other information.

Thank you very much for your help!

globetrotcoma at 2007-7-8 23:12:19 > top of Java-index,Core,Core APIs...
# 5
> Does PersistentBag obey the rules for JavaBeans?You still haven't answered this question. Also, was there anything in it when you encoded?If it doesn't obey the Java Beans rules, you will have to provide a PersistenceDelegate for it.
ejpa at 2007-7-8 23:12:19 > top of Java-index,Core,Core APIs...
# 6
Looks like org.hibernate.collection.PersistentBag doesn't satisfy rules to be a bean; It has no fields.So looks like I'll have to learn about PersistentDelegate.Thank you very much for your help!
globetrotcoma at 2007-7-8 23:12:19 > top of Java-index,Core,Core APIs...
# 7
> Looks like org.hibernate.collection.PersistentBag> doesn't satisfy rules to be a bean; It has no> fields.It doesn't need fields to do that, it needs a nullary constructor and a complete set of getter and setter methods.
ejpa at 2007-7-8 23:12:19 > top of Java-index,Core,Core APIs...