Making a Persistent Class with openJPA and OSGi

From Opentaps Wiki
Revision as of 11:05, 3 March 2012 by Sichen (talk | contribs) (Protected "Making a Persistent Class with openJPA and OSGi": Sysop page [edit=sysop:move=sysop])
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 keep them apart from the Java source files. 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

Next, I will need to add support for persistence to my notes/domain module by modifying its pom.xml. I need to add the openJPA plugin:


I will also need to load my persistence.xml and 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: