Persisting 3rd party objects with JPA

java, jpa, persistence

Solution

Check this and this. In short:

- Create `META-INF/orm.xml`

- Follow (read) the `.xsd`

You don't have to manually map each column - only some specifics (i.e. collections and the id) are required. All fields are assumed to be columns (if the class is mapped). If there are no collections, something like this suffices:

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm 
    http://java.sun.com/xml/ns/persistence/orm_1_0.xsd"
    version="1.0">

    <description>External entities from library X</description>
    <package>com.external.library</package>
    <entity class="SomeClassName">
      <id>..</id>
    </entity>
    <entity class="AnotherClassName">
      <id>..</id>
    </entity>
</entity-mapping>

Note that when specifying `<package>` you don't need fully-qualified names.

In case you want a file named differently than `orm.xml`, in your `persistence.xml` specify it via:

<mapping-file>customMappingFile.xml</mapping-file>

Problem

In my current project I am using a 3rd party library which has no JPA annotations. How can I persist objects from that library using JPA and external mappings?

Original source