import java.rmi.*; import javax.ejb.*; public interface HelloWorldCMP extends EJBObject { public String getLanguage() throws RemoteException; public void setLanguage(String language) throws RemoteException; public String getMessage() throws RemoteException; public void setMessage(String message) throws RemoteException; }
import java.rmi.*; import javax.ejb.*; public interface HelloWorldCMPHome extends EJBHome { public HelloWorldCMP create(Integer no) throws RemoteException, CreateException; public HelloWorldCMP findByPrimaryKey(Integer primaryKey) throws RemoteException, FinderException; }
import java.rmi.*; import javax.ejb.*; public class HelloWorldCMPBean implements EntityBean { EntityContext ctx; public Integer no; public String language; public String message; public String getLanguage() { return language; } public void setLanguage(String language) { this.language = language; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } public Integer ejbCreate(Integer no) throws RemoteException, CreateException, RemoteException { this.no = no; return null; } public void ejbPostCreate(Integer no) throws CreateException, RemoteException { } public void ejbLoad() throws RemoteException { } public void ejbStore() throws RemoteException { } public void ejbRemove() throws RemoveException, RemoteException { } public void ejbActivate() throws RemoteException { } public void ejbPassivate() throws RemoteException { } public void setEntityContext(EntityContext ctx) throws RemoteException { this.ctx = ctx; } public void unsetEntityContext() throws RemoteException { ctx = null; } }
import javax.naming.Context; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import javax.ejb.*; public class HelloWorldCMPClient { public static void main(String[] args) { try { // ネーミングコンテキストの取得 Context initial = new InitialContext(); // JNDI名のルックアップ HelloWorldCMPHome home = (HelloWorldCMPHome)PortableRemoteObject. narrow(initial.lookup("HelloWorldCMP"), HelloWorldCMPHome.class); HelloWorldCMP hello; try { // 1行目作成 hello = home.create(new Integer(1)); hello.setLanguage("日本語"); hello.setMessage("こんにちは 世界"); // 2行目作成 hello = home.create(new Integer(2)); hello.setLanguage("英語"); hello.setMessage("Hello World"); } catch (DuplicateKeyException ex) { System.out.println(ex.toString()); } // noが1のものを検索 hello = home.findByPrimaryKey(new Integer(1)); System.out.println(hello.getLanguage() + " " + hello.getMessage()); // noが2のものを検索 hello = home.findByPrimaryKey(new Integer(2)); System.out.println(hello.getLanguage() + " " + hello.getMessage()); } catch (Exception e) { e.printStackTrace(); } } }
C:\作業フォルダ\JavaHello>
C:\作業フォルダ\EntityBean>java HelloWorldCMPClient 日本語 こんにちは 世界 英語 Hello World
C:\作業フォルダ\EntityBean>