user509755
user509755

Reputation: 2991

How to share Persistence.xml between ejb module and web module in EAR?

We have one EAR application with one ejb module which contains all enity beans and few session beans and pojos. It has one persistence.xml. Now there is other web module which is spring mvc app and it uses entity beans in ejb modules. Now to use jpa I have to create one persistence.xml in web module too. But it is duplicated as we already have in ejb module. Is there any way I can keep only one persistence.xml in ear and will be used in both ejb and web module. Can somebody give me simple project structure as an example. Just for knowledge we are using maven.

Thanks

Upvotes: 2

Views: 3946

Answers (3)

tzimnoch
tzimnoch

Reputation: 216

I had difficulty implementing this with Maven. If you will be using Maven to package your project, the following setting will put shared-between-ejb-and-web modules dependencies (anything in the ear's pom's dependencies defined as <type>jar</type>) in the /lib directory of your ear file:

<plugin>
    <artifactId>maven-ear-plugin</artifactId>
    <configuration>
        <defaultLibBundleDir>lib/</defaultLibBundleDir>
    …
</plugin>

Upvotes: 0

user509755
user509755

Reputation: 2991

Here is sample of my persistence.xml and applicationContext.xml http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

<persistence-unit name="acme" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>java:/acmeDS</jta-data-source>
    <properties>
        <property name="hibernate.hbm2ddl.auto" value="none" />
        <property name="hibernate.show_sql" value="true" />
        <property name="jboss.entity.manager.factory.jndi.name" value="persistence-units/acmeManager"/>
         <property name="jboss.entity.manager.jndi.name" value="persistence-units/acme"/>
    </properties>
</persistence-unit>

And Spring context.xml

<jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence-units/acme" />
<bean
    class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

Upvotes: 0

sebastiencol
sebastiencol

Reputation: 113


You can put your persistence.xml along with your entity classes into a separate jar that you put in the /lib folder of you EAR.

yourEar.ear
    |
    |_ yourEjb.jar
    |
    |_ yourWeb.war
    |
    |_ lib
        |
        |_ yourPersistence.jar
               |
               |_ META-INF/persistence.xml
               |
               |_ yourEntity.class

Upvotes: 3

Related Questions