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

4 Comments

  1. Hi,
    Nice post.
    One Question:
    Does your EJB project has persistence.xml in it?
    or you maintain only one persistence file per whole project?

    • Hi Sprinivas,
      thank you for your comment. The EJB project does not have persistence.xml in it, the only persistence.xml file in whole EAR package is in project which is packaged as JAR and put inside of the EAR during build (it is referenced from EAR’s pom.xml as described in the post).

  2. Thanks for the post !

    I tried your solution but I still have an error saying that there is no EJB in the EJB module….

    If I has @Stateless on one class, it works… Did you had to do that to?

    • Hi Steph,
      In my case I did not need to add additional EJB to the common-ejb project, it only contains entity classes (@javax.persistence.Entity) and some utility classes.

      I suppose you receive the exception at deployment time. What application server do you use?

Leave a Comment