Book Image

Java EE 7 Developer Handbook

By : Peter A. Pilgrim
Book Image

Java EE 7 Developer Handbook

By: Peter A. Pilgrim

Overview of this book

<p>The seventh edition of the Enterprise Java platform is aimed at helping Java engineers take advantage of the advancements in HTML5 and web standards. Web Sockets, asynchronous input and output with Servlets, and strong type safety through the CDI containers will ensure that Java EE 7 remains popular for server-side applications.<br />If you are a user aiming to get acquainted with the Java EE 7 platform, this book is for you.</p> <p>"Java EE 7 Developer Handbook" provides a solid foundation of knowledge for developers to build business applications. Following the lead of Agile practices, there is a focus on writing tests to demonstrate test-driven development principles, using the embedded GlassFish 4.0 container examples and the Gradle build system. You will learn about CDI, EJB, JPA, JMS, MDB, Servlets, WebSocket, JAX-RS, Bean Validation, and so much more.</p> <p>"Java EE 7 Developer Handbook" is designed as a companion to the professional software developer who quickly needs to lookup some working code, understand the basics of the framework, and then go out and fulfill the business contract with the customer. Typically, engineers are under pressure to develop professional code that is of high quality and contains a low number of bugs. Java EE 7 Developer Handbook relies heavily on the Arquillian framework to illustrate how much easier it is to write Java EE tests, and together with the modern practice of writing containerless applications that actually embed an application container, developing agile Java EE suddenly becomes reasonable, smart, pragmatic, and achievable.</p> <p>You will start off with an overview of the Java EE platform: the containers, the design, and architecture. From there, you can follow the path of the CDI, the true gem of the framework, and then the server side end point, EJB. It is completely up to you when and if you want to learn about Java persistence. However, don’t miss out on the highlights of Java EE 7 such as WebSocket, Bean Validation, and asynchronous Servlet API.</p> <p>"Java EE 7 Developer Handbook" is a vertical slice through standard Java enterprise architecture. If you have been wondering why developers have invested so much time and effort into learning topics such as Enterprise Java Beans, you will quickly understand why when you find out the difference between stateful and stateless Beans. Best of all, this book covers the topic from the perspective of new API and new modern practices. For instance, you, the developer and designer, are expected to write applications with annotations in comparison with J2EE. Java EE 7 Developer Handbook incorporates helpful hints and tips to get the developer up to speed in a short amount of time on EJB, CDI, Persistence, Servlet, JMS, WebSocket, JAX-RS and Bean Validation, and much more.</p> <p>"Java EE 7 Developer Handbook" is the reference guide you need beside you at your desk.</p>
Table of Contents (23 chapters)
Java EE 7 Developer Handbook
Credits
About the Author
Acknowledgment
About the Reviewers
www.PacktPub.com
Preface
Index

Persistence unit


The persistence.xml file is a simple XML deployment descriptor file that defines a persistence unit. This file configures the name of the EntityManager service, which manages a set of entity instances. The XML configuration also defines how each EntityManager connects to the database. A persistence unit may declare one or more uniquely named EntityManager instances.

The root element of the persistence.xml file is the persistence XML element and it contains a set of persistence-unit XML complex elements.

Here is a simple declaration of persistence.xml for JPA 2.1:

<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://www.oracle.com/webfolder/technetwork/
    jsc/xml/ns/persistence/persistence_2_1.xsd"">
  
    <persistence-unit name=""trading"" 
    transaction-type=""JTA"">
        <jta-data-source>jdbc/trading</jta-data-source>
    </persistence-unit>	
    <persistence-unit name=""audit"" 
    transaction-type=""JTA"">
        <jta-data-source>jdbc/audit</jta-data-source>
    </persistence-unit>
</persistence>

The transaction-type XML element for a persistence unit can be either JTA or RESOURCE_LOCAL. Specifying the values, JTA indicates that the entity manager partakes in distributed transactions, whereas RESOURCE_LOCAL indicates that the database transactions are just local to the particular application running in that Java EE application server on this JVM.

The element jta-data-source defines a JTA data source configured in the application server. The element non-data-source defines a non-JTA data source.

The element provider defines a particular persistence provider. Of course, the necessary configuration must be applied to the application server, or it should be supplied already by the product.

Here is an example of a persistence unit with both JPA properties and vendor properties:

<persistence version=""2.1"" ...>
  <persistence-unit name=""ratings"" transaction-type=""JTA"">
  <provider>org.hibernate.ejb.HibernatePersistence
  </provider>
    <non-jta-data-source>jdbc/rating</non-jta-data-source>
    <properties>
    <property name=""javax.persistence.schema-generation.database.action"" value=""create""/>
    <property name=""javax.persistence.schema-generation.scripts.action"" value=""drop-and-create""/>
    <property name=""javax.persistence.schema-generation.scripts.create-target"" value=""/tmp/create-sql.ddl""/>
    <property name=""javax.persistence.schema-generation.scripts.drop-target"" value=""/tests/drop-sql.ddl""/>
    <property name=""hibernate.flushMode"" value=""FLUSH_AUTO"" />
    <property name=""hibernate.hbm2ddl.auto"" value=""none"" />
    </properties>
  </persistence-unit>
</persistence>

The <properties> element is a container of <property> elements that configure a name and value pair. In the preceding example, the standard properties are prefixed with javax.persistence. The persistence provider is set to Hibernate JPA and there are a couple of properties defined that are only relevant to that ORM solution, namely hibernate.flushmode and hibernate.hbm2ddl.auto.

XML schema documents for Java EE 7

Oracle has reorganized the location of XML Schema Definitions for Java EE 7. All of the schemas are in present in the link http://xmlns.jcp.org/xml/ns/javaee and effectively they are under the auspices of the Java Community Process. For XML validation of resources like persistence.xml, you can find more information on exact XSD locations at http://www.oracle.com/webfolder/technetwork/jsc/xml/ns/javaee/index.html.

Properties

JPA 2.1 specification defines a set of standard properties that allows the application to optionally generate schemas or execute scripts in connection to the data source. The <properties> element in the persistence.xml file specifies both standard and vendor specific properties.

Here is a table of the JPA 2.1 properties; the property must be prefixed with javax.persistence to complete the correct name:

Property Name

Description

schema-generation.create-script-source

Specifies the name of a pre-packaged application script or a file that creates tables, views, or user SQL functions.

schema-generation.drop-script-source

Specifies the name of a pre-packaged application script or a file that drops tables, views, or user SQL functions.

schema-generation.sql-load-script-source

Specifies the name of a pre-packaged application script or a file that loads data into database tables.

schema-generation. database.action

Informs the persistence provider how to generate the schema for the persistence unit. The valid values are none, create, drop-and-create, and drop.

schema-generation. scripts.action

Informs the persistence provider how to generate scripts with Database Definition Language (DDL) for the persistence unit. The valid values are none, create, drop-and-create, and drop.

schema-generation.create-source

Informs the persistence provider about the ordering of processing during the startup of the persistence unit regarding object-relational mapping metadata, DDL script, or both. The valid values are metadata, script, metadata-then-script, or script-then-metadata.

schema-generation.drop-source

Informs the persistence provider about the ordering of processing during the shutdown of the persistence unit regarding object-relational mapping metadata, DDL script, or both. The valid values are metadata, script, metadata-then-script, or script-then-metadata.

jdbc.driver

Fully qualified name of the driver class (Java SE only).

jdbc.url

Driver specific URL (Java SE only).

jdbc.user

Database connection username (Java SE only).

jdbc.password

Database connection password (Java SE only).

lock.timeout

Specifies a hint for pessimistic lock timeout in milliseconds.

query.timeout

Specifies a hint for query timeout in milliseconds.

validation.group.pre-persist

Defines groups of entities that are targeted for validation upon the pre-persist event.

validation.group.pre-update

Defines groups of entities that are targeted for validation upon the pre-update event.

validation.group.pre-remove

Defines groups of entities that are targeted for validation upon the pre-remove event.

XML representation of object-relational mapping

As an alternative to annotations on entities, JPA supports object-relational mapping from an XML. The file is called orm.xml that is located in the /META-INF folder of the persistence unit. Developers can also explicitly name the XML mapping file using the mapping-file element. The XML representation can be useful for third-party library entities that predate annotation capabilities in Java SE 5 and where the business has no control of the source code access.

Here is a persistence unit that illustrates the concept:

<persistence version=""2.1"" ... >
    <persistence-unit name=""legacyTrading"" transaction-type=""JTA"">
        <jta-data-source>jdbc/legacyDB</jta-data-source>
    <mapping-file>my-orm.xml</mapping-file>
    <jar-file>LegacyTradeEntities.jar</jar-file>
    <class>com.fxtradelib.Trade.class</class>
    <class>com.fxtradelib.Order.class</class>
    <class>com.txtradelib.Counterpart.class</class>
    </persistence-unit>
</persistence>

The jar-file element indicates that the persistence provider also searches the additional JAR file for managed persistence entities. The class element explicitly identifies provider Java types that are entity classes, embeddable classes, and mapped super classes.