Porting Oracle’s “First Cup” Tutorial to Wildfly / JBoss

  JBoss, Tutorial, Wildfly

Persistence Settings

The following persistence.xml file comes from the archetype which we used for the firstcup-war project.

<?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="firstcupPU" transaction-type="JTA">
      <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
      <jta-data-source>java:comp/DefaultDataSource</jta-data-source>
      <properties>
         <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
      </properties>
   </persistence-unit>
</persistence>

Obviously, Oracle’s tutorial used ‘EclipseLink’ for persistance. We want to use ‘Hibernate’ instead. Both persistance API’s are JPA 2.1 compatible. The database we want to use is the H2 in-memory database. So forget about the above persistence.xml. See the correct persistence.xml file we need to deploy:

<?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="PUnit" transaction-type="JTA">
      <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
      <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
      <class>org.firstcup.war.entity.FirstcupUser</class>
      <properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />
         <property name="hibernate.max_fetch_depth" value="3" />
         <property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
         <property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
         <property name="hibernate.hbm2ddl.auto" value="create" />
         <property name="hibernate.show_sql" value="true" />
      </properties>
   </persistence-unit>
</persistence>

Handling the H2 database is a bit tricky. First, in a production environment, you should remove the hibernate.show_sql property. In order to initially create the H2 database / table, the property hibernate.hbm2ddl.auto property contains the value ‘create’. After initially launching the firstcup-war project, further launches should contain the value “update” in this property.

LEAVE A COMMENT