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?

-------------Problems Reply------------

The question is a little too broad for me to answer. But I do have specific related experience using Jackson under JAXB with JPA entities that you might find interesting.

In my case, I had a JPA model with roughly three dozen entities and lots of cyclic references. The graph of relationships between entities was also almost strongly connected. In other words, it was possible to navigate to nearly any entity in the set from any other by following entity relationships. In my case, with entities as described and Jackson 1.5, overlaying JAXB annotations on my JPA entities turned out to be a bad idea.

For one thing, Jackson 1.5 got into infinite recursion on the cyclic references. I consider that operator error rather than a bug. Jackson is awesome software. Also, I think the upcoming 1.6 release provides new features to handle this per JACKSON-235. So this might be moot soon!

My other difficulty related to serialized compactness in the face of strongly connected entities. Serializing all my entity relationships was impractical. I would have serialized an obscene amount of irrelevant information in every request by naively following all entity relationships to their full depth.

I wanted to specify multiple serializations of my JAXB objects, choosing one with appropriate fields and relationships depending on the intended use. But, as far as I'm aware, JAXB and Jackson offer no such flexibility. They offer significant flexibility in defining the representation -- what's transient, how lists look, etc. -- but I don't think multiple representations are possible for one object. Maybe there's a clever way to define multiple representations under JAXB or Jackson and switch at runtime... I'd be interested to learn if such a thing exists. Perhaps there's a feature for this that I'm ignorant of, or some trickery that can be played with subclassing. But I couldn't find it, so ultimately I gave up and went with DTOs.

Again, this is all pretty specific to the model. Maybe these are non-issues for you (or maybe you have clever solutions for these problems!)

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:


DataNucleus allows persistence using JPA to RDBMS (using JDBC behind the scenes) and XML (using JAXB behind the scenes). It can interprete your JPA annotations as defining how the JAXB serialisation is performed - you could add JAXB annotations too if you so wished

Category:java Views:0 Time:2010-07-17

Related post

  • Good JAXB design pattern to wrap model and or convert model (POJO)? 2011-10-25

    I have the following model class MyClass { id someRandomString } I would like to return this POJO to the client as... <Root> <random>if + randomstring</random> </Root> Basically I'm dealing with a very message oriented service

  • JPA Mapping Issue - Modelling direction requested 2010-08-09

    I'm having a problem modelling the following problem in JPA. I have a JPA Entity class 'User', like so: (accessors/mutators/extraneous fields/extraneous JPA configuration omitted for brevity) @Entity class User { @Id @Generated Value long id; @OneToM

  • Mapping JAXB data to a Domain Model 2011-03-14

    I get an InputStream from a web services call, and I use JAXB to convert it to objects, so I do not have to parse the xml. The objects contain a bunch of data I am not interested in, and of the data I care about, I want to instantiate a custom domain

  • How can we combine predictors from two different linear models into one? 2012-02-14

    I was trying to automate a final step model building. I would like to combine predictors from two separate models into one final model. I played around with update.formula() but realized I can update an old lmfit$call to a new one, e.g update.formula

  • Should Nginx Be Combined With Language Supporting Asynchronous Programming Model? 2010-07-25

    I found there are a lot of articles comparing Nginx and Apache in Internet. However, all these comparisons are based on stress test to web server running PHP code. I suppose this is mainly due to Apache is generally deployed with PHP as LAMP architec

  • Ruby On Rails 3, order by the combination of two values in different models 2011-03-26

    Hi I wonder if there's a way to do this without involving advance SQL code? Model 1. Product :has_many prices Model 2. Retailer :has_many prices :has_one profile Model 3. Price - price:integer :belongs_to :retailers :belongs_to :products Model 4. Pro

  • Combine JAXB and JAXWS for an imported XML Schema 2011-03-30

    How can I specify a JAXB Binding for an imported XSD within a WSDL when using wsimport? I tried following binding, which causes the error "XPath evaluation of //xs:element[@name='isFoobar'] results in an empty target node". <?xml version="1.0" enc

  • Using JPA and JSON serialization 2010-09-03

    I would like to have a JPA Entity that has a GSon Object as a field/attribute. For instance: @Entity public class MyEntity { private JsonObject myObject; public JsonObject getObject() { return myObject; } public void setObject(JsonObject obj) { myObj

  • How to reference external JPA/JAXB domain model project? 2012-02-08

    I am developing a client/server application currently, where the server consists of a RESTful interface (jersey) and the client is a JSF application. Both are running on a glassfish 3.1.1 server. To persist some data and produce XML output I created

  • Do Domain Classes usually get JPA or JAXB Annotations or both? 2009-11-30

    I have a Java enterprise application that provides a web service, has a domain layer, and a hibernate persistence layer. In this particular case, there is not a huge difference (at the moment) between the objects I send over the wire, the domain obje

  • Mapping xml to jpa entities using JAXB 2011-12-06

    Isn't it possible to map xml to jpa entities using JAXB? Will Eclipselink Moxy be helpful? --------------Solutions------------- Note: I'm the EclipseLink JAXB (MOXy) lead and a member of the JAXB 2 (JSR-222) expert group. Yes you can map JPA entities

  • XSD Schema - JAXB marshaling - Datastore(JPA/JDO) Roundtrip 2010-11-03

    I'm trying to find a way to accomplish a xsd schema to datastore roundtrip, with minimum effort. I used jaxb to build my object model from schemas, now I would like to store these objects based on JPA (or JDO or something else?). Is it possible, to a

  • how to generate JMX MXBean compatible java class model from JAXB? 2010-06-11

    From the JMX MXBean specification, a java class type J must satisfy Either if J has at least one public constructor with a ConstructorProperties annotation, Or if J has a public no-arg constructor, and for every getter in J with type T and name N the

  • JAXB & JPA annotations. Are there any strong arguments for preferring field access over property access? 2011-01-17

    I'm starting a new project that's using both JAXB and JPA annotations. I've read with interest the posts discussing the pros and cons of mixing both JAXB and JPA annotations in a single class. Are there any guidelines describing when to use field lev

  • JPA 2 XML Mapping of Embedded so that it worked with Hibernate Meta model generator 2011-06-01

    I want to use JPA 2 Meta model generator for Hibernate (Version 1.1.1-Final) (in a Spring Application). Because I use a Mapped Superclass which is the base for all Entities, and this class is located in an different jar (for reuse) I need to map this

  • Django - combining forms for related models 2011-07-13

    Is there a common method/best practice/any means for combining forms that span multiple related models? I want to create/edit model objects along with other, related model objects on the same page. Basically, being able to create/edit one model insta

  • Preventing Netbeans JAXB generation trashing classes 2010-05-02

    I'm developing a SOAP service using JAX-WS and JAXB under Netbeans 6.8, and getting a little frustrated with Netbeans trashing my work every time the XSD schema my JAXB bindings are based upon changes. To elaborate, the IDE automatically generates cl

  • jndi database connection with jpa and eclipselink 2010-09-20

    I try to setup a database connection in java with JNDI in combination with eclipseLink/JPA on Tomcat 5.5. I already configured the JNDI resource in web.xml and context.xml. The db connection works with JNDI without using JPA and eclipseLink. After co

  • How does the JPA handle partial, non-disjoint inheritance (using InheritanceType.JOINED in class-per-table scenario) along with EntityManager.find()? 2010-11-10

    I have a problem modeling/understanding partial, non-disjoint inheritance with JPA annotations. Here are the four tables: CREATE TABLE Persons ( id INTEGER NOT NULL, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, PRIMARY KEY (id));

Copyright (C), All Rights Reserved.

processed in 0.151 (s). 11 q(s)