Maven EAR project with common persistence context

Recently I run into a use case where i had to replace simple WAR project by an EAR project containing multiple WAR projects (customer required a new application operating on the same DB schema). There were 2 things i needed to share among WAR files in the EAR:

  • Hibernate entity classes
  • persistence context

Making the Hibernate entity classes available among EAR project
First step was creating a Maven project of EJB packaging type. I have put the entity classes inside of this project. The pom.xml was updated to contain following code:

...
<artifactId>common-ejb</artifactId>
<packaging>ejb</packaging>
...
<build>
  <plugins>
    <plugin>
      <artifactId>maven-ejb-plugin</artifactId>
      <version>2.3</version>
      <configuration>
        <!-- Tell Maven we are using EJB 3.1 -->
        <ejbVersion>3.1</ejbVersion>
      </configuration>
    </plugin>
  </plugins>
</build>

Second, I have added dependancy to the pom.xml in the WAR files which needed to access shared entity classes.

<dependency>
  <groupId>com.bitfiction</groupId>
  <artifactId>common-ejb</artifactId>
  <type>ejb</type>
  <scope>provided</scope>
</dependency>

Share persistence context among the EAR project
To share the container managed persistance unit inside the EAR without need of multiple persistence.xml files in each WAR file, I created a project of JAR packaging type:

...
<artifactId>common-persistence</artifactId>
<packaging>jar</packaging>
...
<build>
  <resources>
    <resource>
      <directory>src/main/resources</directory>
    </resource>
  </resources>
</build>

And put the persistence.xml inside of the src/main/resources folder of the JAR project.
persistence.xml
Then I have added the JAR project as a jarModule inside of the EAR project’s pom.xml under the maven-ear-plugin configuration section:

<build>
  <finalName>main-ear</finalName>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-ear-plugin</artifactId>
      <version>2.9</version>
      <configuration>
        <defaultLibBundleDir>lib</defaultLibBundleDir>
        <modules>
          <jarModule>
            <groupId>com.bitfiction</groupId>
            <artifactId>common-persistence</artifactId>
            <bundleDir>lib</bundleDir>
          </jarModule>
        </modules>
      </configuration>
    </plugin>
  </plugins>
</build>

Finally I moved the Recources.java into the EJB project (the same where entity classes were moved) and gained access to the entity manager operating on “primary” persistence unit from each class managed by CDI (Weld since i use JBoss in this project).

public class Resources {
  @SuppressWarnings("unused")
  @Produces
  @PersistenceContext(unitName = "primary")
  private EntityManager em;
}

So this was a brief text about how to get Maven EAR project with common persistence context working. I hope it can help someone, good luck!

Share this Post: Facebook Twitter Pinterest Google Plus StumbleUpon Reddit RSS Email

Related Posts

Leave a Comment