Making a Persistent Class with openJPA and OSGi

From Opentaps Wiki
Jump to navigationJump to search

As I'm working with the Notes application, I decide to make the in the notes/domain module persistent, so we don't have to have a separate class in notes/impl/repository just for persisting.

The first step is to add the openJPA persistence markups to, which describe how the Java fields will be represented in the database:

public class Note implements Serializable {

    private static final long serialVersionUID = -4314958909722739985L;

    @GeneratedValue(strategy=GenerationType.AUTO, generator="uuid-type4-hex")
    @Column(name = "NOTE_ID", nullable = false, length = 32)
    private String noteId;
    @Column(name = "NOTE_TEXT", nullable = false)
    private String noteText;

    @Column(name = "CREATED_BY_USER_ID")
    private String createdByUserId;

    @Column(name = "USER_ID_TYPE")
    private String userIdType;

    @Column(name = "SEQUENCE_NUM", nullable = false)
    private Long sequenceNum;
    @Column(name = "CLIENT_DOMAIN")
    private String clientDomain;
    @Column(name = "DATE_TIME_CREATED")
    private Timestamp dateTimeCreated;
// ... etc

Then, I need to create a META-INF/persistence.xml for this class. Since there currently isn't one in the module, I need to create it from scratch. These xml files are in the src/main/resources directory, to follow Maven rule for project resources. Here's what this file looks like:

<persistence xmlns="" version="2.0">

    <persistence-unit transaction-type="JTA" name="notes">


            <property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/>
            <property name="openjpa.BrokerImpl" value="non-finalizing" /> 


(Ok, I cheated: I moved over the one from notes/impl/repository module and modified it for this class instead of

openJPA performs best when persistent classes have been run through the bytecode enhancer. We will not ignore this great opportunity and add enhancing step into notes/domain POM:


Despite the fact that META-INF/persistence.xml is common place and name for persistence descriptor we also need to add <Meta-Persistence> header to meet specs for persistence bundle. Also, make sure that the org.opentaps.notes.domain package is exported:


Finally, I need to add in dependencies for building the notes/domain module with openJPA:


The first dependency is javax.persistence API, the second one javax.validation, and the third our own package for data validation.

Now when I build, my annotations are accepted, but I get this odd error:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project notes.repository.api: Compilation failure
[ERROR] Failure executing javac, but could not parse the error:
[ERROR] An exception has occurred in the compiler (1.6.0_29). Please file a bug at the Java Developer Connection (  
after checking the Bug Parade for duplicates. Include your program and the following diagnostic in your report.  Thank you.
[ERROR]$CompletionFailure: class file for javax.persistence.InheritanceType not found

Wow! You mean Java has bugs too?

A little searching and guessing, and it turns out that because the notes/api/repository and notes/api/services modules now use a class ( which is persistent, they need their own reference to openJPA persistence as well. So, after some trial and error, it seems that adding this dependency fixes it:


The rest of the refactoring is completed by adding this dependency to other modules, and becomes a persistent class.