Mindmajix

Configuring data persistence

Configuring data persistence

After our short excursus on the EJB remote client Api, we will further enhance our application adding a Persistence Context to it so that we are able to store the key-value pairs on a relational database instead of keeping them in memory. Again, weremind you that the purpose of it is not to teach the theory behind Data Persistence, but rather to show how to configure it within your applications.

As you probably know, a major enhancement in Java EE spec 3.x technology is the addition of the new Java Persistence API, which simplifies the Entity Persistence model. In the Java Persistence API, what used to be called Entity Beans in EJB 2.x are now simply called Entities. The persistence subsystem is included by default into all server configurations:
<extension module=”org.jboss.as.jpa”/>
<subsystem xmlns=”urn:jboss:domain:jpa:1.0″>
</subsystem>
The javax.persistence Api are not loaded by default in the application server.However, as soon as the application server will detect the main configuration file (named persistence.xml) or persistence annotations, the persistence Api will be automatically linked to your application. So, let’s add the JPA persistence.xml configuration file to our project, which will reference the datasource used for mapping our entities to the database:
<?xml version=”1.0″ encoding=”UTF-8″?>

<persistence version=”2.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_2_0.xsd”>
<persistence-unit name=”persistenceUnit” transaction-type=”JTA”>
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:/MySqlDS</jta-data-source>
<properties>
<property name=”hibernate.dialect”
value=”org.hibernate.dialect.MySQLDialect” />
</properties>
</persistence-unit>
</persistence>
The key attributes of this file are the persistence unit name, which will identify its unique name, and the jta-data-source, which must match with a valid datasource definition. In the earlier chapter, we have defined this datasource bound to a MySQL database.

Screenshot_1

Note

The persistence.xml can specify either a JTA datasource or a non-JTA datasource.The JTA datasource is expected to be used within the EE environment (even when reading data without an active transaction). Finally, the properties element can contain any configuration property for theunderlying persistence provider. Since JBoss AS usesHibernate as the EJB3 persistence provider, you can pass in any Hibernate options here.Once created, this file needs to be placed in the META-INF folder of your source classes, as shown in the following screenshot:

Tip

The real path of persistence.xml file

Please note that the Eclipse src/META-INF virtual path will be translated at runtime into the WEB-INF/classes/META-INF of your web application.

Using a default datasource for the JPA subsystem

In this example, we are referencing the datasource from within the persistence.xml, thus following a “canonical” approach well-known to many developers.

You can, however, choose a default datasource for all your JPA applications, by adding the default-datasource element into the JPA subsystem:

            <subsystem xmlns=”urn:jboss:domain:jpa:1.0″>

            <jpa default-datasource=”java:/MySqlDS”/>

            </subsystem>

This way, all JPA applications that haven’t defined the jta-data-source element in persistence.xml will use the default-datasource configured in the main server configuration file.

0 Responses on Configuring data persistence"

Leave a Message

Your email address will not be published. Required fields are marked *

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.