Spring Framework on OpenShift: persistence.xml and EntityManager Configuration on Wildfly 10 and MySQL

Running an Web Application with Spring Framework 4.2.x with Spring-Data-JPA and MySQL on OpenShift the RedHat Cloud is good with Wildfly10.

But how to configure the MySQL Database for Spring-Data-JPA?

Spring-Data-JPA with Wildfly10 and MySQL on Openshift 

First Step: edit persistence.xml. Here is an Example:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> <persistence-unit name="mySimpleWorkListPU" transaction-type="JTA"><jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>
<class>org.woehlke.simpleworklist.entities.ActionItem</class>
<class>org.woehlke.simpleworklist.entities.Category</class>
<class>org.woehlke.simpleworklist.entities.TimelineDay</class>
<class>org.woehlke.simpleworklist.entities.TimelineMonth</class>
<class>org.woehlke.simpleworklist.entities.TimelineYear</class>
<class>org.woehlke.simpleworklist.entities.RegistrationProcess</class>
<class>org.woehlke.simpleworklist.entities.UserAccount</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<!-- Bind entity manager factory to JNDI at java:jboss/myEntityManagerFactory -->
<property name="jboss.entity.manager.factory.jndi.name"
  value="java:jboss/mySimpleWorkListEntityManagerFactory" /> 
<property name="jboss.entity.manager.jndi.name"
  value="java:/mySimpleWorkListEntityManager"/>
</properties>
</persistence-unit>
</persistence>


set <jta-data-source>java:jboss/datasources/MySQLDS</jta-data-source>

and the Properties to:

<property name="jboss.entity.manager.factory.jndi.name"
 value="java:jboss/mySimpleWorkListEntityManagerFactory" /> 
<property name="jboss.entity.manager.jndi.name" value="java:/mySimpleWorkListEntityManager"/>

Second Step: Edit the Spring Application Context (XML):

<jpa:repositories base-package="org.woehlke.simpleworklist" />

<!-- Look up the database in JNDI -->
<jee:jndi-lookup
jndi-name="java:jboss/datasources/ExampleDS"
id="dataSource"expected-type="javax.sql.DataSource" /> 

<!-- Look up the container deployed EntityManager -->
<jee:jndi-lookup
jndi-name="java:/mySimpleWorkListEntityManager"
id="entityManager"
expected-type="javax.persistence.EntityManager" />

<!-- Look up the container deployed EntityManager -->
<jee:jndi-lookup
jndi-name="java:jboss/mySimpleWorkListEntityManagerFactory"
id="entityManagerFactory"
expected-type="javax.persistence.EntityManagerFactory" /> 

<!-- JPA Configuration -->
<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />

<bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" /> <!-- Use JTA transactions with the container-deployed EntityManager -->
<tx:jta-transaction-manager /> 

<!-- Transactions -->
<tx:annotation-driven transaction-manager="transactionManager" />


The Beans dataSource, entityManager and entityManagerFactory are configured to be fetched via JNDI.

Webapp URL: http://jbosswildfly-simpleworklist.rhcloud.com/

Github Source-Code: https://github.com/phasenraum2010/simpleworklist

JPA on Slideshare: http://de.slideshare.net/Thomas_Woehlke/jpa-java-persistence-api-4557033


Kommentare

Beliebte Posts aus diesem Blog

Java EE 7 Petclinic

Simulated Evolution - Artificial Life and DNA

Diffusion-limited aggregation