Serializing a part of object graph

Posted by Felix on Stack Overflow See other posts from Stack Overflow or by Felix
Published on 2010-03-30T11:42:45Z Indexed on 2010/03/30 11:53 UTC
Read the original article Hit count: 315

Filed under:
|
|

Hi all,

I have a problem regarding Java custom serialization. I have a graph of objects and want to configure where to stop when I serialize a root object from client to server.

Let's make it a bit concrete, clear by giving a sample scenario. I have Classes of type

Company
Employee (abstract)
Manager extends Employee
Secretary extends Employee
Analyst extends Employee
Project

Here are the relations:
Company(1)---(n)Employee
Manager(1)---(n)Project
Analyst(1)---(n)Project

Imagine, I'm on the client side and I want to create a new company, assign it 10 employees (new or some existing) and send this new company to the server. What I expect in this scenario is to serialize the company and all bounding employees to the server side, because I'll save the relations on the database. So far no problem, since the default Java serialization mechanism serializes the whole object graph, excluding the field which are static or transient.

My goal is about the following scenario. Imagine, I loaded a company and its 1000 employees from the server to the client side. Now I only want to rename the company's name (or some other field, that directly belongs to the company) and update this record. This time, I want to send only the company object to the server side and not the whole list of employees (I just update the name, the employees are in this use case irrelevant). My aim also includes the configurability of saying, transfer the company AND the employees but not the Project-Relations, you must stop there.

Do you know any possibility of achieving this in a generic way, without implementing the writeObject, readObject for every single Entity-Object? What would be your suggestions?

I would really appreciate your answers. I'm open to any ideas and am ready to answer your questions in case something is not clear.

© Stack Overflow or respective owner

Related posts about java

Related posts about custom