< Previous | Next >

Lesson 1.3: Create an Entity class and a data source for data persistence

Lesson 1.3 leads you through the creation of an Entity class and a database for data persistence.
Before you begin, you must complete Lesson 1.2.
In this lesson you will
  1. Add code to the entity class:
    1. Open JPACounterEntity.java in the Java™ editor, replace all the code with this code, and press CTRL+S to save:
      // This program may be used, executed, copied, modified and distributed
      // without royalty for the purpose of developing, using, marketing, or distributing.
      
      package com.ibm.websphere.ejb3sample.counter;
      
      import javax.persistence.Entity;
      import javax.persistence.Id;
      import javax.persistence.Table;
      
      
      @Entity
      @Table(name="EJB3COUNTERTABLE")
      
      public class JPACounterEntity {
      
          @Id
          private String primarykey = "PRIMARYKEY";
      
          private int value = 0;
      
          public void setValue( int newValue )
          {
              System.out.println ("JPACounterEntity:setValue = " + newValue);
              value = newValue;
          }
      
          public int getValue()
          {
              System.out.println ("JPACounterEntity:getValue = " + value);
              return value;
          }
      
          public void setPrimaryKey( String newKey )
          {
              System.out.println ("JPACounterEntity:setPrimaryKey = '" + newKey + "'");
              primarykey = newKey;
          }
      
          public String getPrimaryKey()
          {
              System.out.println ("JPACounterEntity:getPrimaryKey = '" + primarykey + "'");
              return primarykey;
          }
      }
    2. In the Enterprise explorer view, navigate to EJBCounterSample/ejbModule/META-INF. Right-click on META-INF and select File > New > File. Type persistence.xml in the File name field, and click Finish. The persistence.xml file opens in the Editor. Select source, and copy and paste this code into the source window:
      <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0"
              xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
              <persistence-unit name="Counter">
                     <jta-data-source>jdbc/EJB3SampleDatasource</jta-data-source>
                     <class>com.ibm.websphere.ejb3sample.counter.JPACounterEntity</class>
                     <exclude-unlisted-classes>true</exclude-unlisted-classes>
              </persistence-unit>
      </persistence>
  2. Define a data source
    1. In the Enterprise explorer view, right click EJBSampleEAR, and select Java EE Tools > Open WebSphere Application Server Deployment:
      Open WebSphere Application Server Deployment
    2. In the Websphere Deployment editor, select Derby JDBC Provider (XA), and in the Data source defined in the JDBC provider selected above: field, click Add:
      Data Source definition
    3. In the Create Data Source page, select Derby JDBC Provider (XA), and click Next:
      Create Data Source page
    4. On the Select type of JDBC provider to create page, in the Name field, type EJBCounterSample Data source. In the JNDI name field, type jdbc/EJB3SampleDatasource, and click Next:
      Select the type of JDBC provider to create page
    5. In theCreate Resource Properties page, highlight databaseName property field, and in the Value field, type databases/EJB3SampleDB, and click Finish:
      Derby database definition
You now are ready to move on to Exercise 1.4, Create a Web project to test your application.
< Previous | Next >

Feedback