演習 1.3 では、データ・パーシスタンスのエンティティー・クラスとデータベースの作成について取り上げます。
// This program may be used, executed, copied, modified and distributed
// without royalty for the purpose of developing, using, marketing, or distributing.
package com.ibm.example.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;
}
}
<?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="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="Counter">
<jta-data-source>jdbc/EJB3SampleDatasource</jta-data-source>
<class>com.ibm.example.websphere.ejb3sample.counter.JPACounterEntity</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
</persistence-unit>
</persistence>