Pregunta

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.

¿Fue útil?

Solución

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

Here's a tutorial to get you started:

Otros consejos

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

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top