Skip to content

HyperJAXB3 Generating persistence unit descriptor

Laurent Schoelens edited this page May 17, 2024 · 1 revision

Generating the persistence unit descriptor

According to the entity packaging guidelines of the JPA specification, Hyperjaxb3 generates the META-INF/persistence.xml file which describes generated classes in a persistence unit. This allows easier usage of mapped classes - entity manager will pick up the classes automatically.

Hyperjaxb3 generates the persistence unit in the META-INF/persistence.xml file in the target directory. You may configure an alternative location TODO if you wish.

Persistence unit generated by Hyperjaxb3 simply lists classes which were made entities:

META-INF/persistence.xml

<persistence
    version="1.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="org.jvnet.hyperjaxb3.ejb.tests.po">
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.Items</class>
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.Items$Item</class>
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.USAddress</class>
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.PurchaseOrderType</class>
    </persistence-unit>
</persistence>

Persistence unit in named after the package of the schema-derived classes. If there are several packages, they'll be colon-separated (just lice JAXB context path). You may also specify the name of the persistence unit expliciltly in configuration.

You may also turn off the generation of persistence.xml TODO.

Creating a custom persistence unit descriptor

Augmenting a provided persistence.xml

One of the ways to create a custom persistence unit descriptor is letting HJ3 inject its definitions into an existing persistence.xml template. To do this, you have to set the persistenceXml configuration element in Ant or Maven. Below is an example for Maven:

<plugin>
    <groupId>org.jvnet.hyperjaxb3</groupId>
    <artifactId>maven-hyperjaxb3-plugin</artifactId>
    <configuration>
        <persistenceXml>src/main/etc/persistence.xml</persistenceXml>
    </configuration>
</plugin>

The persistenceXml configuration element should point to the persistence.xml template:

src/main/etc/persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="1.0"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="##generated" transaction-type="RESOURCE_LOCAL">
    <mapping-file>org/jvnet/hyperjaxb3/ejb/tests/po/orm.xml</mapping-file>
    </persistence-unit>
</persistence>

In this case HJ3 will not create the persistence.xml file from scratch, instead it will parse the provided template and inject its definitions (class or mapping-file elements) into the right places:

target/generated-sources/xjc/META-INF/persistence.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<persistence version="1.0"
  xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd
 http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
  xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://java.sun.com/xml/ns/persistence">
    <persistence-unit name="org.jvnet.hyperjaxb3.ejb.tests.po" transaction-type="RESOURCE_LOCAL">
        <mapping-file>org/jvnet/hyperjaxb3/ejb/tests/po/orm.xml</mapping-file>
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.Items</class>
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.Items$Item</class>
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.PurchaseOrderType</class>
        <class>org.jvnet.hyperjaxb3.ejb.tests.po.USAddress</class>
    </persistence-unit>
</persistence>
Clone this wiki locally