jQuery in Action, 2nd edition*
The moose likes Object Relational Mapping and the fly likes Combine JAXB + JPA? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Combine JAXB + JPA?" Watch "Combine JAXB + JPA?" New topic
Author

Combine JAXB + JPA?

Ryan Kumsher
Greenhorn

Joined: Aug 16, 2010
Posts: 3
I am creating REST services to insert users into a database using JERSEY, JAXB, and JPA. I am new to all 3 technologies...

Currently I am accepting a JAXB user object.
Converting to a JPA user object using a helper class I wrote
Then persisting the JPA user



Is there an easy way (automatic code generation hopefully) to combine the JAXB and JPA classes so I don't have to convert the JAXB user class into a JPA user class? This way I could just call em.persist(user) on the object passed in to the addUser method. It looks like EclipseLink Moxy might be of use here but couldn't find a good example...

Thanks
James Sutherland
Ranch Hand

Joined: Oct 01, 2007
Posts: 553
You can use the same objects/class in both JAXB and JPA. You can include annotations for both JPA and JAXB to map the same class to both XML and a database.

EclipseLink supports both JPA and JAXB in a single product.

This depends on what data you are sending through XML and to the database though. If the data is very different, then you may wish to have different objects. If there are no major differences in the data, then since both JAXB and JPA allow configurable mappings, you should be able to use both.

See,
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA

TopLink : EclipseLink : Book:Java Persistence : Blog:Java Persistence Performance
Blaise Doughan
Greenhorn

Joined: Aug 25, 2010
Posts: 8
Here is a series of blog posts I put together on combining JAXB & JPA in a RESTful Service:

- Database Model (http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-15.html)
- JPA Model (http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-25.html)
- Apply XML Binding (http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-35.html)
- RESTful Service (http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-45.html)
- Client (http://bdoughan.blogspot.com/2010/08/creating-restful-web-service-part-55.html)

Also see,
- http://wiki.eclipse.org/EclipseLink/Examples/REST
Ryan Kumsher
Greenhorn

Joined: Aug 16, 2010
Posts: 3
Blaise,

Thanks a lot for the in depth posts. This was real helpful for me in learning some of the basics of JPA and JAXB. I'm looking forward to implementing this technique and getting rid of my ugly helper class I wrote to convert between the 2.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Combine JAXB + JPA?