Jump to: navigation, search

Difference between revisions of "EclipseLink/Examples/MySports"

m
m (Installation and Configuration)
Line 25: Line 25:
 
Currently, the '''MySports''' example is only available through SVN access. At a later date, a downloadable ZIP file that contains the examples will be made available.  
 
Currently, the '''MySports''' example is only available through SVN access. At a later date, a downloadable ZIP file that contains the examples will be made available.  
  
#Prepare the prerequisites:
 
  
**GlassFish 3.1 (http://glassfishplugins.java.net/eclipse36/)  
+
=== Prequisites ===
**Eclipse Java EE with GlassFish adapter (http://www.eclipse.org/downloads/)
+
To run the MySports example, you will need the following:
** EclipseLink 2.3.0 OSGi Bundles http://www.eclipse.org/eclipselink/downloads/index.php
+
# GlassFish 3.1 (http://glassfishplugins.java.net/eclipse36/)  
 +
# Eclipse Java EE with GlassFish adapter (http://www.eclipse.org/downloads/)
 +
# EclipseLink 2.3.0 OSGi Bundles (http://www.eclipse.org/eclipselink/downloads/)
  
#Configure your Eclipse environment:  
+
 
##Add a variable named '''eclispelink_2.3_jar''' for the javaagent usage of the test project. You can add this variable by creating a String Substitution variable, as shown here:<br>[[Image:Mysports variable.png]]  
+
=== Upgrade and Configure GlassFish===
##Ensure you have a GlassFish Server configured in the Java EE perspective. <br> [[Image:Mysports glassfishserver.png]]
+
Use this procedure to upgrade GlassFish to use the current EclipseLink version and prepare the connection pool.
#Configure GlassFish
+
#Replace the following JAR files from '''<GLASSFISH_HOME>\glassfish\modules\''' with the JAR files from the EclipseLink OSGI bundle:
##EclipseLink 2.3 bundles installed in GlassFish
+
*org.eclipse.persistence.antlr.jar
##A connection pool named 'jdbc/MySports' must be defined in the server
+
*org.eclipse.persistence.asm.jar
#Get the example's projects  
+
*org.eclipse.persistence.core.jar
 +
*org.eclipse.persistence.jpa.jar
 +
*org.eclipse.persistence.jpa.modelgen.jar
 +
*org.eclipse.persistence.oracle.jar
 +
#Copy the following JAR file from the EclipseLink OSGI bundle to  '''<GLASSFISH_HOME>\glassfish\modules\''':
 +
*org.eclipse.persistence.moxy_2.3.0.v20110604-r9504.jar
 +
#Use the GlassFish Admin Console to create a new JDBC resource named '''jdbc/MySports''' and a new JDBC connection pool named '''MySportsPool'''. In this example, we will use the embedded Derby database:
 +
* Resource type: javax.sql.DataSource
 +
* Datasource classname: org.apache.derby.jdbc.EmbeddedDataSource40
 +
'''Note''': Be sure to edit the JDBC Connection Pool properties to reflect your database conneciton information. In this example, we will use:
 +
* User: APP
 +
* Password: APP
 +
* ServerName: localhost
 +
* DatabaseName: gemini
 +
* PortNumber: 1527
 +
 
 +
 
 +
===Configure Eclipse Environment ===
 +
Now you can configure Eclipse to run the MySports example.
 +
#Add a variable named '''eclispelink_2.3_jar''' with the value '''<ECLIPSELINK OSGI BUNDLE>\org.eclipse.persistence.core_2.3.0.v20110604-r9504.jar'''. <br />This is used for the javaagent of the test project. You can add this variable by creating a String Substitution variable, as shown here: <br />[[Image:Mysports variable.png]]  
 +
#Obtain the three example projects from the EclipseLink SVN respository:
 
#*http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/my-sports/MySports/  
 
#*http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/my-sports/MySports/  
 
#*http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/my-sports/MySportsAdmin/  
 
#*http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/my-sports/MySportsAdmin/  
 
#*http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/my-sports/MySportsTests/  
 
#*http://dev.eclipse.org/svnroot/rt/org.eclipse.persistence/trunk/examples/my-sports/MySportsTests/  
##Import the projects  
+
'''Note''': If you use the Eclipse Team Provider for SVN plugin (http://www.eclipse.org/subversive/downloads.php) Eclipse will can import the example projects.
##Customize the '''eclipselink-examples-mysports.properties''' file for your database  
+
#Create a new Derby connection profile. Use the same JDBC Connection Properties that you used when creating the GlassFish connection pool.
#Run The example  
+
#Add the Connectivty Driver Defintition for the Derby datbase to the project.
##Run the '''MySports AllTests''' launch target. This will create the database tables and prepare an initial population  
+
#Start the Derby server. The GlassFish installation includes a Derby instance that can be used, in the '''<GLASSFISH_HOME>\javadb\''' folder.
##Run the '''MySports Admin''' application  
+
#Import the EclipseLink MySports projects into Eclipse.
##Run the '''MySports''' application
+
#Edit the '''eclipselink-examples-mysports.properties''' file to match your database connection information.
 +
 
 +
 
 +
==Running the Exmple==
 +
Now, you're ready to run the MySports example.
 +
#Run the '''MySports AllTests''' launch target. This will create the database tables and prepare an initial population  
 +
#Run the '''MySports Admin''' application  
 +
#Run the '''MySports''' application
  
 
== Example Details ==
 
== Example Details ==

Revision as of 07:11, 28 June 2011

EclipseLink MySports Example

The MySports example is a simple web application which demonstrates several key features of

EclipseLink including:

  • EclipseLink JPA
    • @Multitenant(SINGLE_TABLE)
    • Extensible entities with @VirtualAccessMethods

Example Overview

The example includes 3 projects

  1. MySports: A Dynamic Web project which deploys the primary application exposing a JSF and JAX-RS (REST) front end.
  2. MySports Admin:
  3. MySports Tests: A Java project containing Junit tests that verify the functionality of

Installation and Configuration

Currently, the MySports example is only available through SVN access. At a later date, a downloadable ZIP file that contains the examples will be made available.


Prequisites

To run the MySports example, you will need the following:

  1. GlassFish 3.1 (http://glassfishplugins.java.net/eclipse36/)
  2. Eclipse Java EE with GlassFish adapter (http://www.eclipse.org/downloads/)
  3. EclipseLink 2.3.0 OSGi Bundles (http://www.eclipse.org/eclipselink/downloads/)


Upgrade and Configure GlassFish

Use this procedure to upgrade GlassFish to use the current EclipseLink version and prepare the connection pool.

  1. Replace the following JAR files from <GLASSFISH_HOME>\glassfish\modules\ with the JAR files from the EclipseLink OSGI bundle:
  • org.eclipse.persistence.antlr.jar
  • org.eclipse.persistence.asm.jar
  • org.eclipse.persistence.core.jar
  • org.eclipse.persistence.jpa.jar
  • org.eclipse.persistence.jpa.modelgen.jar
  • org.eclipse.persistence.oracle.jar
  1. Copy the following JAR file from the EclipseLink OSGI bundle to <GLASSFISH_HOME>\glassfish\modules\:
  • org.eclipse.persistence.moxy_2.3.0.v20110604-r9504.jar
  1. Use the GlassFish Admin Console to create a new JDBC resource named jdbc/MySports and a new JDBC connection pool named MySportsPool. In this example, we will use the embedded Derby database:
  • Resource type: javax.sql.DataSource
  • Datasource classname: org.apache.derby.jdbc.EmbeddedDataSource40

Note: Be sure to edit the JDBC Connection Pool properties to reflect your database conneciton information. In this example, we will use:

  • User: APP
  • Password: APP
  • ServerName: localhost
  • DatabaseName: gemini
  • PortNumber: 1527


Configure Eclipse Environment

Now you can configure Eclipse to run the MySports example.

  1. Add a variable named eclispelink_2.3_jar with the value <ECLIPSELINK OSGI BUNDLE>\org.eclipse.persistence.core_2.3.0.v20110604-r9504.jar.
    This is used for the javaagent of the test project. You can add this variable by creating a String Substitution variable, as shown here:
    Mysports variable.png
  2. Obtain the three example projects from the EclipseLink SVN respository:

Note: If you use the Eclipse Team Provider for SVN plugin (http://www.eclipse.org/subversive/downloads.php) Eclipse will can import the example projects.

  1. Create a new Derby connection profile. Use the same JDBC Connection Properties that you used when creating the GlassFish connection pool.
  2. Add the Connectivty Driver Defintition for the Derby datbase to the project.
  3. Start the Derby server. The GlassFish installation includes a Derby instance that can be used, in the <GLASSFISH_HOME>\javadb\ folder.
  4. Import the EclipseLink MySports projects into Eclipse.
  5. Edit the eclipselink-examples-mysports.properties file to match your database connection information.


Running the Exmple

Now, you're ready to run the MySports example.

  1. Run the MySports AllTests launch target. This will create the database tables and prepare an initial population
  2. Run the MySports Admin application
  3. Run the MySports application

Example Details

MySports Domain (example.mysports.model)

The domain model is that of any arbitrary sports league. The intent is to build an application that captures the state of an arbitrary league and use it in a multitenant application where multiple leagues (tenants) can be hosted. The domain model is intentionally unaware of the potential support for multiple leagues and therefore only models entities that exist within an individual league.

Class/Interface Description
Division
@Entity
@Table(name = "mys_div")
@Multitenant(MultitenantType.SINGLE_TABLE)
@TenantDiscriminatorColumn(name = "LEAGUE_ID", contextProperty = LEAGUE_CONTEXT)
@NamedQueries({
    @NamedQuery(name="Division.findAll", query="SELECT d FROM Division d ORDER BY d.name",
                hints={@QueryHint(name=QueryHints.QUERY_RESULTS_CACHE, value=HintValues.TRUE)}),
    @NamedQuery(name="Division.findByName", query="SELECT d FROM Division d WHERE d.name = :NAME")
})
public class Division implements Extensible {
Team
@Entity
@Table(name = "mys_team")
@Multitenant(MultitenantType.SINGLE_TABLE)
@TenantDiscriminatorColumn(name = "LEAGUE_ID", contextProperty = LEAGUE_CONTEXT)
@NamedQuery(name="Team.findByDivisionAndName", query="SELECT t FROM Team t WHERE t.name = :NAME AND t.division.name = :DIV")
public class Team implements Extensible {
Player
/**
 * In the MySports demo a Player entity represents an individual member of a
 * team. The Player entity is both multitenant and extensible.
 */
@Entity
@Table(name = "mys_player")
@Multitenant(MultitenantType.SINGLE_TABLE)
@TenantDiscriminatorColumn(name = "LEAGUE_ID", contextProperty = LEAGUE_CONTEXT)
@VirtualAccessMethods
public class Player implements Extensible {
Extensible An application interface used to indicate extensible types and enable their use in the presentation tier. This interface is not required or used by EclipseLink.
Divisions A non-entity container class used for returning multiple divisions from a JAX-RS call to enable the MOXy marshalling into XML.

Persistence

The persistence layer is the main purpose of this example application. The persistence layer makes use of application bootstrapped persistence contexts with support for an EntityManagerFactory and backing ServerSession (with Cache) per league (tenant). This all bootstrapped off of a single persistence-unit definition.

Runtime Architecture

Mysports persistence.png

LeagueRepository

The LeagueRepository is the primary persistence abstraction responsible for league (tenant) specific persistence access managing both JPA persistence units and the MOXy JAXBContext.

JPA Usage

MOXy (JAXB) Usage

The application makes use of EclipseLink MOXy to map its persistent entities into XML within the JAX-RS (RESTful) interface. The mapping is done using an eclipselink-oxm.xml mapping file instead of JAXB annotations.