Contents

Overview

This example demonstrates how to derive an XML representation from a set of JPA entities using JAXB. In simplest terms JAXB will be used to apply a tree structure to the object graph of JPA entities. Multiple tree representations of an object graph are possible and will depend on the root object chosen and the direction the relationships are traversed.

JPA Entities

For this example the following JPA entities will be used. In order to save space package names, import statements, and the get /set methods have been omitted from the following code samples. All annotations shown below are standard JPA annotations.

Privately Owned Relationships

These relationships apply when the target object(s) is only referenced by a single source object. This type of relationship can be safely represented as nesting in XML (the default in JAXB).

One To One

In JPA the OneToOne annotation indicates that only one instance of the source entity is able to refer to the same target entity instance. Since the relationship between Employee and Address is bi-directional, we need to mark one side transient in JAXB to prevent a cycle.

One To Many

In JPA the OneToMany annotation indicates that only one instance of the source entity is able to refer to the same target entity instance. Since the relationship between Employee and Address is bi-directional, we need to mark one side transient in JAXB to prevent a cycle.