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.

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

3 Comments

upvoted for mention of configuration by exception and keys, otherwise tool-dependence is not the best way to solve the problem
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.
@Mowgli - MOXy like any JAXB (JSR-222) implementation can start from java classes or XML schema.
2

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

Here's a tutorial to get you started:

3 Comments

I have been searching for ages for a tutorial, thank you very much. I will give this a go.
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.
At present, Hyperjaxb3 does not support bi-directional JPA2 relationships.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.