Reputation: 13
I have to send detached JPA entities from server to client and it is not coming back, however client should be able to reliably instantiate these entities.
Entity has bidirectional relationships (cyclic graph), collections, arrays and embeddable keys in it.
I have control over both server and the client - i.e. I can use same versions of the entity classes in client and server and the serialized data will be very short lived.
This is going to be used for a critical operation I must ensure the client should be able to deserialize without any problem. Risk free - at any cost. Reliability of deserialization is more important than efficiency/speed/size
Any recommendations on how to approach this and what library to use for a minimum risk solution? There seem to be so many options XStream, JAXB, Java serialization, Json, XMLEncoder etc... I am a bit confused.
Upvotes: 1
Views: 918
Reputation: 149047
Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB (JSR-222) expert group.
EclipseLink MOXy is an implementation of JAXB (JSR-222) specification. As EclipseLink also provides a JPA implementation many of its extensions are aimed at mapping JPA entities:
@XmlInverseReference
for supporting bidirectional relationships (see: http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html)@XmlPath
for mapping embedded IDs (see: http://blog.bdoughan.com/2010/07/xpath-based-mapping.html).@XmlJoinNodes
(similar to JPA's @JoinColumns
) when you need to map by key/foreign key.Since your question is also tagged json you may be interested to know that MOXy also provides JSON-binding using the JAXB metadata:
Speaking to reliability MOXy is currently the default JAXB provider in WebLogic 12.1.1.
Upvotes: 1
Reputation: 11501
I have good experience using JAXB for XML serialization of JPA entity classes. Proven on many critical projects for client-server communication based on Web services.
Usualy it's good practice to use another DTO layer for serialization of entities. This way you can cut cycles in relationships and further customize your objects for serialization. You can map between these layers by hand, or use some tool like Dozer.
Typically it involves just creation of corresponding POJOs with few JAXB annotations (@XmlType etc.) and some mapping between these POJO DTOs and JPA Entities.
Upvotes: 1