Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining JAXB and JPA in one model

I have to design a data model (in a Java EE 6 application) that will be persisted via JPA, and that also needs to be serialized via JAXB. The last time I did that, I had one group of entity classes with JAXB annotations, and another with JPA annotations. This meant that I had to have a lot of boilerplate code for translating between the two. I'm thinking of combining them, so that each class will have both types of annotations. I know this can be done, but my question is, should it be? Will it cause any problems?

like image 759
Mike Baranczak Avatar asked Jul 17 '10 00:07

Mike Baranczak


1 Answers

This can definitely be done. I actually find the prospect of maintaining code to copy between the models more problematic.

EclipseLink is a great choice for this application as it contains both a JPA (EclipseLink is the RI and open sourced from TopLink), and a JAXB implementation.

EclipeLink JAXB (MOXy) also contains a number of extensions for mapping JPA entities to XML:

  • XPath base mappings
  • Mapping bi-directional relationships (using @XmlInvereseReference)
  • Handling composite key relationships
  • Handling embedded key classes
  • Handling java.sql.* types

For more information see:

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
like image 187
bdoughan Avatar answered Nov 15 '22 09:11

bdoughan