views:

107

answers:

3

Hi everyone,

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

Can you recommend a good example? So far I only found information on external XML-Mappings and JPA in the JSR220 spec which mostly states the structure of the mappings. I would of course prefer a real example.

+1  A: 

Refer to the docs of your JPA implementation; any serious JPA implementation should provide examples of use of XML as well as annotations. See http://www.datanucleus.org/products/accessplatform_2_0/jpa/metadata_xml.html for DataNucleus docs for XML structure, and then refer to the particular relation types for examples of different features.

DataNucleus
Thanks. I'm currently using hibernate and will have a look at their doc. Just thought, that since JPA is a standard persistence mechanism there should be some provider-agnostic tutorial floating around somewhere.
er4z0r
The DN docs are in general provider-agnostic, and always add "DataNucleus extension" where something is an extension.
DataNucleus
+1  A: 

Check this and this. In short:

  1. Create META-INF/orm.xml
  2. 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>
Bozho
Thanks for your tips. Do you happen to know, if there is some kind of generator for those mappings, that can analyze class files and spit out the mappings. Doing it manually is possible, but for a rather big domain it sounds like unnecessary manual work. Call me lazy, but I like to automate whatever I can ;-)
er4z0r
@er4z0r check my update.
Bozho
Hmm... How is the PK "discovered"? I think you need to define the `<id>` somewhere.
Pascal Thivent
@Pascal Thivent yes, thanks. I'm using it currently with `<embeddable>`, so I've missed it. Updated.
Bozho
Hey thanks for your help :-)
er4z0r
+1  A: 

As pointed out, you can use JPA mapping file instead of annotations to map, well, non annotated entities (e.g. classes from a third party library). Follow any JPA tutorial based on mapping files to get started.

Regarding automation, I don't think you can automate the generation of orm.xml from the object model (as opposed to a physical model, an object model doesn't contain enough information, for example which field is the PK or, for a bi-directional association, which side is the owner, etc hence the need for metadata). But most IDEs provide support for this, e.g. Eclipse.

Pascal Thivent