Home » Java » Enterprise Java » Registering entity types with OpenJPA programmatically

About Dan Haywood

Registering entity types with OpenJPA programmatically

I’ve just started work on an OpenJPA objectstore for Isis. In the normal scheme of things, one would register the entity types within the persistence.xml file. However, Isis is a framework that builds its own metamodel, and can figure out for itself which classes constitute entities. I therefore didn’t want to have to force the developer to repeat themselves, so the puzzle became how to register the entity types programmatically within the Isis code.

It turns out to be pretty simple, if a little ugly. OpenJPA allows implementations of certain key components to be defined programmatically; these are specified in a properties map that is then passed through to javax.persistence.Persistence.createEntityManager(null, props). But it also supports a syntax that can be used to initialize those components through setter injection.

In my case the component of interest is the openjpa.MetaDataFactory. At one point I thought I’d be writing my own implementation; but it turns out that the standard implementation does what I need, because it allows the types to be injected through its setTypes(List<String>) mutator. The list of strings is passed into that property as a ;-delimited list.

So, here’s what I’ve ended up with:

final Map<String, String> props = Maps.newHashMap();

final String typeList = entityTypeList();
  "org.apache.openjpa.persistence.jdbc.PersistenceMappingFactory(types=" + typeList + ")");

// ... then add in regular properties such as 
// openjpa.ConnectionURL, openjpa.ConnectionDriverName etc...
entityManagerFactory = Persistence.createEntityManagerFactory(null, props);

where entityTypeList() in my case looks something like:

private String entityTypeList() {
    final StringBuilder buf = new StringBuilder();
    // loop thru Isis' metamodel looking for types that have been annotated using @Entity
    final Collection<ObjectSpecification> allSpecifications = 
    for(ObjectSpecification objSpec: allSpecifications) {
        if(objSpec.containsFacet(JpaEntityFacet.class)) {
            final String fqcn = objSpec.getFullIdentifier();
    final String typeList = buf.toString();
    return typeList;

Comments welcome, as ever

Reference: Registering entity types with OpenJPA programmatically from our JCG partner Dan Haywood at the Dan Haywood blog blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.