Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert XML to database with Java

Need: take in XML and save data to database.

Currently using: JAXB to convert the XML Schema to java classes. Then I intend to use JPA to persist the objects marshalled by JAXB.

Problem: I want something to bridge the gap. After JAXB generates the Java classes I have to manually annotate all java.util.Date fields with @Temporal; I have to put @Entity on the top of every generated class...etc.

I came across Hyperjaxb. But I can find little documentation on it, and can't get it to work.

I am open to completely different approaches. This seems like it would be a common problem, so maybe there is a generic solution.

like image 404
Kevin Avatar asked Oct 10 '22 05:10

Kevin


1 Answers

Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.

If you already have an existing database schema, the you could use the Dali tool in Eclipse (part of the Web Tools Project) to generate your JPA entities form the database:

  • http://www.eclipse.org/webtools/dali/

JAXB is configuration by exception, this means you only need to add annotations where you want to override the default behaviour. Dali also has tooling to make adding JAXB annotations easier:

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics

JPA entities sometimes use bidirectional relationships and composite keys, these can be tricky to map to XML. EclipseLink JAXB (MOXy) contains some extensions to make this easier (note EclipseLink also offers a JPA implementation):

  • http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
like image 111
bdoughan Avatar answered Oct 12 '22 23:10

bdoughan