5

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.

Kevin
  • 4,070
  • 4
  • 45
  • 67

3 Answers3

3

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:

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:

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):

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • upvoted for mention of configuration by exception and keys, otherwise tool-dependence is not the best way to solve the problem – necromancer Dec 18 '11 at 19:58
  • 1
    I had seen MOXy, but it seemed that it was only useful for going: Database->Java->XML and I need XML->Java->Database. I'll have another look to make sure, thanks. – Kevin Dec 18 '11 at 20:32
  • @Mowgli - MOXy like any JAXB (JSR-222) implementation can start from java classes or XML schema. – bdoughan Dec 18 '11 at 20:49
2

Hyperjaxb does exactly what you're trying to achieve. Here's the documentation:

Here's a tutorial to get you started:

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • I have been searching for ages for a tutorial, thank you very much. I will give this a go. – Kevin Dec 18 '11 at 20:32
  • The confluence server was down yesterday, that is why I didn't find the tutorial. Thanks for the link, as i would have given up otherwise. – Kevin Dec 18 '11 at 20:41
  • At present, Hyperjaxb3 does not support bi-directional JPA2 relationships. – Nick Jan 06 '14 at 16:01
0

I can suggest two options: Option 1. Define your entity types separately, having the relevant JPA annotations, generate your JAXB types from the schema, and at runtime, map one to the other. If it is a simple mapping you can use Apache BeanUtils to just copy over the attributes from one bean to the other, if it is a more complex mapping then you can use a framework like dozer

Option 2: Start from entity types, generate the schema from your entity types or manually keep the entity types and the schema in synch. More like the option that you have described, except that the authoritative source is the java code than the schema.

Biju Kunjummen
  • 49,138
  • 14
  • 112
  • 125
  • 1
    Thanks for you reply. The classes are very big, and I think both these options will have me writing a lot of code that is very similar to the auto-generated code. However, I will resort to something like this if I can't find another solution. – Kevin Dec 18 '11 at 04:43