Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/JPA/Tomcat Web Tutorial"

m (EclipseLink JPA Deployed on Tomcat 6 using Eclipse WTP)
m (EclipseLink JPA Deployed on Tomcat 6 using Eclipse WTP)
Line 2: Line 2:
  
 
'''<font color="red">Started 20080808 - under construction for the next week</font>'''
 
'''<font color="red">Started 20080808 - under construction for the next week</font>'''
 
  
 
Tomcat 6 is not a JEE5 compliant server, however the servlet container is able to run EJB 3.0/JPA applications in application-managed SE (stand alone) mode.
 
Tomcat 6 is not a JEE5 compliant server, however the servlet container is able to run EJB 3.0/JPA applications in application-managed SE (stand alone) mode.
Line 79: Line 78:
 
*It is not recommended that the WAR include its own version of eclipselink.jar.
 
*It is not recommended that the WAR include its own version of eclipselink.jar.
 
*Your eclipse project should reference but not include this jar.
 
*Your eclipse project should reference but not include this jar.
 
  
 
===JDBC JAR location===
 
===JDBC JAR location===
Line 94: Line 92:
 
===Persistence.xml===
 
===Persistence.xml===
 
* Make sure that your persistence.xml (and optionaly orm.xml) file is placed off of the src/META-INF directory and not the default WebContent/META-INF dir so that it gets picked up by the servlet classloader from the classes directory.
 
* Make sure that your persistence.xml (and optionaly orm.xml) file is placed off of the src/META-INF directory and not the default WebContent/META-INF dir so that it gets picked up by the servlet classloader from the classes directory.
 
+
* Entity classes must be explicitly listed as they will not be automatically discovered.
 +
* You may use standard SE jdbc elements for the db url, username, password - I have shown a JTA datasource example.
 +
* Notice there is no target-server element for the Tomcat 6 web container.
 +
<source lang="xml">
 +
<?xml version="1.0" encoding="UTF-8"?>
 +
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 +
<persistence-unit name="statCreateTables" transaction-type="JTA">
 +
  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
 +
  <jta-data-source>java:comp/env/ds/OracleDS</jta-data-source>
 +
  <!-- non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source-->
 +
  <class>org.eclipse.persistence.example.unified.business.StatClass</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatAbstractClass</class>       
 +
  <class>org.eclipse.persistence.example.unified.business.StatClassType</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatClassContents</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatComposition</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatField</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatInheritance</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatLabel</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatMethod</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatMethodParam</class>
 +
  <class>org.eclipse.persistence.example.unified.business.StatPackage</class>
 +
  <properties>
 +
    <property name="eclipselink.session.customizer" value="org.eclipse.persistence.example.unified.integration.JPAEclipseLinkSessionCustomizer"/>       
 +
    <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.OraclePlatform"/>           
 +
    <property name="eclipselink.logging.level" value="FINEST"/>           
 +
    <!-- property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
 +
    <property name="eclipselink.ddl-generation.output-mode" value="database"/-->
 +
  </properties>
 +
</persistence-unit>
 +
</source>
 
===Start Server===
 
===Start Server===
 
*Steps: Select the WAR and [Run on Server].
 
*Steps: Select the WAR and [Run on Server].
 
*The first "Run on Server" may not start the server, in this case you "Start Server" and then "Run on Server".
 
*The first "Run on Server" may not start the server, in this case you "Start Server" and then "Run on Server".
 +
*Or optionaly, export your WAR to the ''$TOMCAT_HOME/webapps'' directory.
 +
**When the tomcat server is started up (as a service here) it will expand out the WAR into a dir based on its context name.
  
 
===Publish EAR===
 
===Publish EAR===

Revision as of 11:10, 13 August 2008

EclipseLink JPA Deployed on Tomcat 6 using Eclipse WTP

Started 20080808 - under construction for the next week

Tomcat 6 is not a JEE5 compliant server, however the servlet container is able to run EJB 3.0/JPA applications in application-managed SE (stand alone) mode.

If you want to get a small JPA web application running quickly on Tomcat - the services provided by the Web Tools Project pluggin in the Eclipse IDE can take care of the deployment details and set the server into debug mode for you.

This basic example details how to use Eclipse to run/debug a minimum JPA Application Managed web application servlet using EclipseLink JPA as the persistence provider. The goal of this example is to detail the minimum steps needed to run EclipseLink inside Tomcat using the Eclipse IDE - at this point no presentation/controller layer such as JSF, Spring or Struts will be used beyond a basic HttpServlet so we can concentrate on the the integration layer JPA setup.

Development Environment

Software: Eclipse IDE for Java EE 3.4 M5 Ganymede (Feb 2008) with all 5 packages (DTP 1.6, EMF 2.4, GEF 3.4, WTP 3.0, XSD 2.4), Oracle 11g DB 11.1.0.6.0, Java JDK 1.6.0_04, Apache Tomcat 6.0.18

This example will run fine with Eclipse 3.3 EE and any Database that EclipseLink supports.

Prerequisites

  • Install Eclipse EE
    • I installed a clean version of Eclipse Ganymede M5 with all of WTP 3.0
  • Install a Database
    • In this example I am using Oracle 11g, the table schemas have already been created manually and all entitity java classes have been generated using the Eclipse DALI tool.
  • Install the Tomcat 6 Web Container
    • TOMCAT_HOME=C:/opt/tomcat6
    • I installed the version that runs as a windows service

Tomcat configuration Changes

JNDI Datasource Setup

There are 2 steps to configuring a JTA JNDI JDBC Datasource in Tomcat 6.

  • 1) configure a new global resource in conf/server.xml

Add the following <Resource> element to <GlobalNamingResources>.

 <Resource 
  name="jdbc/OracleDS" 
  auth="Container" 
  type="javax.sql.DataSource" 
  maxActive="100" 
  maxIdle="30" 
  maxWait="10000"
  username="scott" 
  password="password" 
  driverClassName="oracle.jdbc.driver.OracleDriver" 
  url="jdbc:oracle:thin:@255.255.255.255:1521:orcl"
  />
  • 2) configure a datasource context for the WAR in conf/server.xml

To link jdbc/OracleDS to ds/OracleDS for WAR consumption, add the following <Context> element inside the <Host> element. The attributes displayName, docBase, path, ResourceLink:global and ResourceLink:name need to be modified.

<Host...>
 <Context 
  className="org.apache.catalina.core.StandardContext"
  cachingAllowed="true"
  charsetMapperClass="org.apache.catalina.util.CharsetMapper"
  cookies="true" crossContext="false" debug="0"
  displayName="unified"
  docBase="C:\opt\tomcat6\webapps\unified.war"
  mapperClass="org.apache.catalina.core.StandardContextMapper"
  path="/unified" privileged="false" reloadable="false"
  swallowOutput="false" useNaming="true"
  wrapperClass="org.apache.catalina.core.StandardWrapper">
   <ResourceLink 
    global="jdbc/OracleDS" 
    name="ds/OracleDS"
    type="javax.sql.DataSource"/>
 </Context>

Persistence JAR location

  • Since Tomcat does not have an EJB container - you must add EJB 3.0/JPA 1.0 capability for EclipseLink JPA by placing the specification jar into the container lib directory $TOMCAT_HOME/lib
  • I put persistence_1_0.xsd there as well to be safe.
  • It is not recommended that the WAR include its own version of persistence.jar.
  • Your eclipse project should reference but not include this jar and xsd.

EclipseLink JAR location

  • The eclipselink.jar should be placed off of the container lib directory $TOMCAT_HOME/lib
  • It is not recommended that the WAR include its own version of eclipselink.jar.
  • Your eclipse project should reference but not include this jar.

JDBC JAR location

Jars for Oracle 11, MySQL 5 and Sybase 5.5/6.0 are off the container lib directory $TOMCAT_HOME/lib

Create server in Eclipse

Open the servers view New | Server | Apache | Tomcat 6.

Create Dynamic Web application

Create your own J2EE WEB Application as below - WAR not EAR. File | new | project | Web | Dynamic Web Project

Persistence.xml

  • Make sure that your persistence.xml (and optionaly orm.xml) file is placed off of the src/META-INF directory and not the default WebContent/META-INF dir so that it gets picked up by the servlet classloader from the classes directory.
  • Entity classes must be explicitly listed as they will not be automatically discovered.
  • You may use standard SE jdbc elements for the db url, username, password - I have shown a JTA datasource example.
  • Notice there is no target-server element for the Tomcat 6 web container.
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
 <persistence-unit name="statCreateTables" transaction-type="JTA">
  <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
   <jta-data-source>java:comp/env/ds/OracleDS</jta-data-source>
   <!-- non-jta-data-source>java:comp/env/ds/OracleDS</non-jta-data-source-->
   <class>org.eclipse.persistence.example.unified.business.StatClass</class>
   <class>org.eclipse.persistence.example.unified.business.StatAbstractClass</class>         
   <class>org.eclipse.persistence.example.unified.business.StatClassType</class>
   <class>org.eclipse.persistence.example.unified.business.StatClassContents</class>
   <class>org.eclipse.persistence.example.unified.business.StatComposition</class>
   <class>org.eclipse.persistence.example.unified.business.StatField</class>
   <class>org.eclipse.persistence.example.unified.business.StatInheritance</class>
   <class>org.eclipse.persistence.example.unified.business.StatLabel</class>
   <class>org.eclipse.persistence.example.unified.business.StatMethod</class>
   <class>org.eclipse.persistence.example.unified.business.StatMethodParam</class>
   <class>org.eclipse.persistence.example.unified.business.StatPackage</class>
   <properties>
    <property name="eclipselink.session.customizer" value="org.eclipse.persistence.example.unified.integration.JPAEclipseLinkSessionCustomizer"/>        
    <property name="eclipselink.target-database" value="org.eclipse.persistence.platform.database.oracle.OraclePlatform"/>            
    <property name="eclipselink.logging.level" value="FINEST"/>            
    <!-- property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
    <property name="eclipselink.ddl-generation.output-mode" value="database"/-->
   </properties>
 </persistence-unit>

Start Server

  • Steps: Select the WAR and [Run on Server].
  • The first "Run on Server" may not start the server, in this case you "Start Server" and then "Run on Server".
  • Or optionaly, export your WAR to the $TOMCAT_HOME/webapps directory.
    • When the tomcat server is started up (as a service here) it will expand out the WAR into a dir based on its context name.

Publish EAR

Perform a JPQL query

  • Console Output
Aug 8, 2008 8:35:56 PM org.apache.catalina.startup.Catalina start
[EL Finest]: 2008.08.08 20:36:02.291--ServerSession(1067475)--Thread(Thread[http-8080-2,5,main])--Begin predeploying Persistence Unit statCreateTables; state Initial; factoryCount 0
[EL Finest]: 2008.08.08 20:36:02.963--ServerSession(32961147)--Thread(Thread[http-8080-2,5,main])--End predeploying Persistence Unit statJPA; state Predeployed; factoryCount 0
[EL Finer]: 2008.08.08 20:36:02.978--Thread(Thread[http-8080-2,5,main])--JavaSECMPInitializer - transformer is null.
[EL Finest]: 2008.08.08 20:36:02.978--ServerSession(1067475)--Thread(Thread[http-8080-2,5,main])--Begin predeploying Persistence Unit statCreateTables; state Predeployed; factoryCount 0
[EL Finest]: 2008.08.08 20:36:02.978--ServerSession(1067475)--Thread(Thread[http-8080-2,5,main])--End predeploying Persistence Unit statCreateTables; state Predeployed; factoryCount 1
[EL Finest]: 2008.08.08 20:36:02.978--ServerSession(1067475)--Thread(Thread[http-8080-2,5,main])--Begin deploying Persistence Unit statCreateTables; state Predeployed; factoryCount 1
[EL Info]: 2008.08.08 20:36:03.572--ServerSession(1067475)--Thread(Thread[http-8080-2,5,main])--EclipseLink, version: Eclipse Persistence Services - 1.0 (Build SNAPSHOT - 20080808)
[EL Info]: 2008.08.08 20:36:04.353--ServerSession(1067475)--Thread(Thread[http-8080-2,5,main])--file:/C:/wse/w34/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/UnifiedTomcatWeb/WEB-INF/classes/-statCreateTables login successful
  • Browser Output

Tomcat eclipselink jpa demo cap.JPG

Back to the top