By default, TomEE uses the OpenJPA, but the hibernate is more commonly used in the software industry. So, I will use the hibernate and the PostgreSQL database.
persistence.xml
The JPA requires a persistence unit defined in a persistence.xml, which has to be located in the META-INF directory. In the maven project created with the default directory structure, the META-INF directory is located under the src/main/resources directory. One persistence.xml file can have more than one persistence unit definition, but I use one persistence unit here. The persistence-unit demoDB uses a JTA for the transaction.
<?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="demoDB" transaction-type="JTA"> <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider> <jta-data-source>demoDS</jta-data-source> <class>demo.model.entity.User</class>
<shared-cache-mode>NONE</shared-cache-mode> <properties> <property name="hibernate.dialect" value="PostgreSQL9"/> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="true" /> <property name="tomee.jpa.factory.lazy" value="true" /> <property name="hibernate.temp.use_jdbc_metadata_defaults" value="false" /> </properties>
</persistence-unit> </persistence>
On this file, I defined a jta-data-source element 'demoDS' but where did we define the DataSource?
resources.xml on TomEE
On the TomEE, a DataSource can be defined in the <TomEE-HOME>/conf/tomee.xml file or in a WEB-INF/resources.xml. This is my Resource declaration in the resources.xml, but more properties, which are optional, are explained in a TomEE website: http://tomee.apache.org/datasource-config.html
<?xml version='1.0' encoding='UTF-8'?> <resources> <Resource id="demoDS" type="javax.sql.DataSource"> jdbcDriver = org.postgresql.Driver jdbcUrl = jdbc:postgresql://localhost:5555/demoDB password = password passwordCipher = PlainText userName = postgres </Resource> </resources>
On the next post, we will make a database connection using this persistence unit.
pom.xml used
This is very minimal pom.xml to save the space. Depends on your needs, you may include more hibernate dependencies. A reference on a TomEE webpage: http://tomee.apache.org/tomee-and-hibernate.html
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jihwan.jee</groupId> <artifactId>demo</artifactId> <version>0.1-SNAPSHOT</version> <packaging>war</packaging> <properties> <tomee.version>7.0.2</tomee.version> <!-- openejb.javaee.api>6.0-6</openejb.javaee.api> --> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <failOnMissingWebXml>false</failOnMissingWebXml> <maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.source>1.8</maven.compiler.source> </properties> <dependencies> <dependency> <groupId>org.apache.tomee</groupId> <artifactId>tomee-embedded</artifactId> <version>${tomee.version}</version> <scope>provided</scope> <exclusions> <exclusion> <groupId>org.apache.openjpa</groupId> <artifactId>openjpa</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>9.4.1212</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.2.10.Final</version> <exclusions> <exclusion> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> </exclusion> <exclusion> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.2_spec</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>5.2.10.Final</version> <exclusions> <exclusion> <groupId>org.hibernate.javax.persistence</groupId> <artifactId>hibernate-jpa-2.1-api</artifactId> </exclusion> <exclusion> <groupId>org.jboss.spec.javax.transaction</groupId> <artifactId>jboss-transaction-api_1.2_spec</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.tomee.maven</groupId> <artifactId>tomee-maven-plugin</artifactId> <version>${tomee.version}</version> <configuration> <tomeeVersion>${tomee.version}</tomeeVersion> <tomeeClassifier>plus</tomeeClassifier> <synchronization> <extensions> <extension>.class</extension> </extensions> </synchronization> <reloadOnUpdate>true</reloadOnUpdate> <systemVariables> <tomee.serialization.class.blacklist>-</tomee.serialization.class.blacklist> </systemVariables> </configuration> </plugin> </plugins> </build> </project>
No comments:
Post a Comment