EJB 3.0 persistence

ejb persistence example and the ejb persistence tutorial
JohenCorner Profile Pic
JohenCorner,France,Professional
Published Date:02-08-2017
Your Website URL(Optional)
Comment
Building an EJB 3.0 Persistence Model with Oracle JDeveloper Oracle Fusion Middleware is a family name for a set of Java EE products that are integrated for SOA and web application deployment. WebLogic Server (WLS) is the Java EE container and Oracle JDeveloper the Java EE and SOA development IDE. In this chapter, we will use JDeveloper to create an EJB 3.0 application, and then we will deploy and test our application leveraging the embedded WebLogic Server that comes with JDeveloper. This makes it very easy for us to develop, deploy, and test our application. WebLogic server 10.x provides some value-added features to facilitate EJB 3 development. WebLogic server 10.x supports automatic deployment of a persistence unit based on the injected variable's name. Thejavax.persistence. PersistenceContext andjavax.persistence.PersistenceUnit annotations are used to inject the persistence context in anEntityManager or EntityManagerFactory variable. A persistence context is a set of entities that are mapped to a database with a global JNDI name. If the name of the injected variable is the same as the persistence unit, theunitName attribute of the PersistenceContext orPersistenceUnit is not required to be specified. The EJB container automatically deploys the persistence unit and sets its JNDI name to be the same as the persistence unit name inpersistence.xml. For example, if the persistence unit name in thepersistence.xml file is em, anEntityManager variable may be injected with the persistence context as follows: PeristenceContext private EntityManager em; aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper We did not need to specify theunitName attribute in thePersistenceContext because the variable name is the same as the persistence unit. Similarly, an EntityManagerFactory variable may be injected with the persistence context as follows,emf being also the persistence unit name: PersistenceUnit private EntityManagerFactory emf; Another value-added feature in WebLogic server 10.x is support for vendor-specic fi subinterfaces of theEntityManager interface. For example, the BEA Kodo persistence provider provides theKodoEntityManager subinterface, which may be injected with the persistence context as follows: PersistenceContext private KodoEntityManager em; Setting the environment Before getting started, we need to install Oracle JDeveloper 11g, which may be downloaded fromhttp://www.oracle.com/technology/products/jdev/index. html. Download the Studio Edition, which is the complete version of JDevloper with all the features. Oracle JDeveloper 11g is distributed as a GUI self-extractor application. Click on thejdevstudio11110install application. The Oracle Installer gets started. Click on Next in the Oracle Installer. Choose a middleware home directory and click on Next. 76 aChapter 4 Choose the Install Type as Complete, which includes the integrated WebLogic Server, and click on Next. Confirm the default Product Installation directories and click on Next. 77 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper The WebLogic Server installation directory is thewlserver_10.3 folder within the middleware home directory. Choose a shortcut location and click on Next. The Installation Summary lists the products that are installed, which include the WebLogic Server and the WebLogic JDBC drivers. Click on Next to install Oracle JDeveloper 11g and the integrated WebLogic Server 10.3. We also need to install the Oracle database 10g/11g or the lightweight Oracle XE, which may be downloaded fromhttp://www.oracle.com/technology/software/ products/database/index.html. When installing Oracle database, also install the sample schemas. Creating a datasource in JDeveloper Next, we create a JDBC datasource in JDeveloper. We shall use the datasource in the EJB 3.0 entity bean for database persistence. First, we need to create a database table in some sample schema,OE for example. Run the following SQL script in SQL Plus: CREATE TABLE Catalog (id INTEGER PRIMARY KEY NOT NULL, journal VARCHAR(100), publisher VARCHAR(100), edition VARCHAR(100), title VARCHAR(100), author VARCHAR(100)); A database table gets created in theOE sample schema. 78 aChapter 4 Next, we need to create a JDBC connection in JDeveloper with Oracle database. Open the Database Navigator or select the Database Navigator tab if already open. Right- click on the IDE Connections node and select New Connection. 79 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper In the Create Database Connection window, specify a Connection Name, select Connection Type as Oracle (JDBC), specify Username as OE, which is the schema in which theCatalog table is created, and specify the password for the OE schema. Select Driver as thin, Host Name as localhost, SID as ORCL, and JDBC Port as 1521. Click on the Test Connection button to test the connection. If the connection gets established, click on OK. The OracleDBConnection gets added to the Database Navigator view. The CATALOG table that we created is listed in the Tables. 80 aChapter 4 Creating an EJB 3 application In this section, we create an EJB 3.0 application in JDeveloper. Select New Application. 81 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper Specify an Application Name, select the Java EE Web Application template, which consists of a Model project and a ViewController project, and click on Next. Next, specify the name (EJB3ViewController) for the View and Controller project. In the Project Technologies tab, transfer the EJB project technology from the Available list to the Selected list using the button. We have selected the EJB project technology, as we shall be creating an EJB 3.0 model. Click on Next. 82 aChapter 4 Select the default Java settings for the View project and click on Next. Configure the EJB Settings for the View project. Select EJB Version as Enterprise JavaBeans 3.0 and select Using Annotations. Click on Next. Next, create the Model project. Specify the Project Name (EJB3Model for example), and in the Project Technologies tab transfer the EJB project technology from the Available list to the Selected list using the button. We have added the EJB project technology, as the EJB 3.0 application client is created in the View project. Click on Next. 83 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper Select the default Java settings for the Model project and click on Next. Similar to the View project, configure the EJB settings for the Model project. Select EJB Version as Enterprise JavaBeans 3.0, select Using Annotations and click on Finish. As we won't be using ajndi.properties file or anejb-jar.xml file , we don't need to select the generate option for thejndi.properties file and the ejb-jar.xml file. 84 aChapter 4 An EJB 3.0 application, which consists of a Model project and aViewController project, get added in the Application tab. Select the EJB3Model project in the Application navigator and select Tools Project Properties. In the Project Properties window, select the Libraries and Classpath node. The EJB 3.0 library should be in the Classpath Entries. 85 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper Select the EJB Module node and select the OracleDBConnection in the Connection drop-down list. The datasource corresponding to the OracleDBConnection is jdbc/ OracleDBConnectionDS. Creating an EJB 3 entity bean In this section we shall map an Oracle database table to an entity bean. Subsequently we shall discuss the components of the entity bean class. Mapping an Oracle database table to an entity bean In this section, we create an EJB 3 entity bean from the Oracle database table CATALOG that we created earlier. Select the EJB3Model project in the Application navigator and select File New. In the New Gallery window, select Categories Business Tier EJB and Items Entities from Tables, and click on OK. 86 aChapter 4 In the Persistence Unit, window, select New to create a new persistence unit. In the New Persistence Unit window specify a persistence unit name (em). Specify JTA DataSource Name as jdbc/OracleDBConnectionDS, which is the datasource name corresponding to the OracleDBConnection connection. Select the default settings for Toplink: Server Platform as WebLogic 10. Click on OK. 87 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper The em Persistence Unit gets created. Click on OK in the Persistence Unit window. Select Type of Connection as Online Database Connection and click on Next. 88 aChapter 4 In the Database Connection Details window, select the OracleDBConnection and click on Next. We had configured a connection earlier, but the database connection can be created implicitly in the Database Connection Details, instead of explicitly. In the Select Tables window, select Schema as OE, Name Filter as %, and check the Auto Query checkbox. Select the CATALOG table and click on Next. 89 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper Select the default settings in the General Options window. The default package name is model. In the Entity Class, select Place member-level annotations on as Fields, and select the Implement java.io.Serializable checkbox. Click on Next. In the Specify Entity Details window, select Table Name as OE.CATALOG. As shown in the following screenshot, specify Entity Name as Catalog and Entity Class as model.Catalog. Click on Next. 90 aChapter 4 The Summary page lists the EJB 3.0 JPA Entity that will be generated. In the Summary Page, click on Finish. The CMP Entity bean class—model.Catalog—gets created. Thepersistence.xml deployment descriptor gets created in theMETA-INF directory. 91 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper The entity bean class The entity bean class is just a POJO class annotated with theEntity annotation. ANamedQuery specifies a findAll query, which selects all the entity instances. An entity bean that has caching enabled is persisted to a database; the entity bean is serialized by caches. Therefore, the entity bean class implements thejava. io.Serializable interface. Specify aserialVersionUID variable that is used by serialization runtime to associate a version number with the serializable class: private static final long serialVersionUID = 7422574264557894633L; The database columns are mapped to entity bean properties, which are defined as private variables. The getter setter methods for the properties are also den fi ed. The identifier property is specified with the Id annotation. TheColumn annotation specifies that the id column is not nullable: Id Column(nullable = false) private long id; By default theid column of typeINTEGER is mapped to a field of type Long. Modify theid field to type long, as usually id values are of primitive type. The entity bean class is listed next: package model; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; Entity NamedQueries( NamedQuery(name = "Catalog.findAll", query = "select o from Catalog o") ) public class Catalog implements Serializable private String author; private String edition; private static final long serialVersionUID = 7422574264557894633L; Id Column(nullable = false) private long id; private String journal; private String publisher; 92 aChapter 4 private String title; public Catalog() super(); public Catalog(String author, String edition, long id, String journal, String publisher, String title) super(); this.author = author; this.edition = edition; this.id = id; this.journal = journal; this.publisher = publisher; this.title = title; public String getAuthor() return author; public void setAuthor(String author) this.author = author; public String getEdition() return edition; public void setEdition(String edition) this.edition = edition; public long getId() return id; public void setId(long id) this.id = id; public String getJournal() return journal; public void setJournal(String journal) this.journal = journal; public String getPublisher() return publisher; 93 aBuilding an EJB 3.0 Persistence Model with Oracle JDeveloper public void setPublisher(String publisher) this.publisher = publisher; public String getTitle() return title; public void setTitle(String title) this.title = title; Thepersistence.xml file is used to define the persistence unit/s, which include a JTA datasource that is used for database persistence. The persistence provider is specified as org.eclipse.persistence.jpa.PersistenceProvider. Thejta- data-source is defined as java:/app/jdbc/jdbc/OracleDBConnectionDS. The eclipselink.target-server property is specied as fi WebLogic_10. Thejavax. persistence.jtaDataSource property is specied as fi java:/app/jdbc/jdbc/ OracleDBConnectionDS, which is just the default mapping JDeveloper uses for the JTA Data Source. Thejava:/app/jdbc prefix gets added to the JTA Data Source specified when creating the persistence unit. The persistence.xml congu fi ration file is listed next: ?xml version="1.0" encoding="windows-1252" ? 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" version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" persistence-unit name="em" provider org.eclipse.persistence.jpa.PersistenceProvider /provider jta-data-source java:/app/jdbc/jdbc/OracleDBConnectionDS /jta-data-source class model.Catalog /class properties property name="eclipselink.target-server" value="WebLogic_10" / property name="javax.persistence.jtaDataSource" value="java:/app/jdbc/jdbc/OracleDBConnectionDS" / /properties /persistence-unit /persistence 94 a