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
- Add code to the entity class, JPACounterEntity.java.
- Create a database, EJB3SampleDB, to persist the counter data.
- Add code to the entity class:
- 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;
}
}
- In the Enterprise explorer view, navigate to EJBCounterSample/ejbModule/META-INF.
Right-click on META-INF and select . 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>
- Define a data source
- In the Enterprise explorer view, right click EJBSampleEAR, and
select :
- 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:
- In the Create Data Source page, select Derby
JDBC Provider (XA), and click Next:
- 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:
- In theCreate Resource Properties page,
highlight databaseName property field, and in the Value field,
type databases/EJB3SampleDB, and click Finish:
You now are ready to move on to Exercise 1.4, Create a Web project
to test your application.