#12 Add a JPA Strategy

open
nobody
None
8
2010-05-05
2010-05-05
Bruce Atherton
No

It would be ideal if Simple could make JPA-managed entities automatically XMLizable with the appropriate notations. I am thinking that a strategy like this would work:

1. On serializing to XML, act as usual unless the contained object being serialized has an @Entity annotation. In that case, pull out the field marked as @Id on the contained object and emit an empty tag with the attribute name and value from @ID.

2. On deserializing, we need to get an EntityManager. Note that one is not needed just for serializing. If the root object has an @Entity tag, get its @Id field. If the XML doesn't have that field as a tag or it is set to 0 (or perhaps some configurable setting), create a new instance. Otherwise find the object with that id using the entity manager. Replace fields as specified by the XML.

Only the references to other entities get a little tricky. If the root object already has a reference to another entity, check to see if its @Id field is the same as what is in the XML. If it is we are done. If it is not, use the entity manager to find the object of that type with the given id and assign it to the field of the root object. Lists and maps and arrays just need to synchronize their objects with the ones in the XML.

Presto: All JPA-persisted beans are now available in XML form. I think this would be a huge boon, especially for those developing REST services.

Discussion

  • Bruce Atherton
    Bruce Atherton
    2010-05-05

    • priority: 5 --> 8