Oracle8i Enterprise JavaBeans Developer's Guide and Reference Release 3 (8.1.7) Part Number A83725-01 |
|
import customer.Customer; import customer.CustomerHome; import javax.naming.InitialContext; import javax.naming.NamingException; import java.rmi.RemoteException; import javax.ejb.RemoveException; import javax.ejb.CreateException; import javax.ejb.FinderException; import java.util.Enumeration; import oracle.aurora.jndi.sess_iiop.ServiceCtx; import java.util.Hashtable; import javax.naming.Context; public class CustomerClient { public static void main(String[] argv) { System.out.println("client is running"); try { if (argv.length != 4) { System.out.println("usage: Client serviceURL
objectName user password"); System.exit(1); } String serviceURL = argv [0]; String objectName = argv [1]; String user = argv [2]; String password = argv [3]; Hashtable env = new Hashtable(); env.put(Context.URL_PKG_PREFIXES, "oracle.aurora.jndi"); env.put(Context.SECURITY_PRINCIPAL, user); env.put(Context.SECURITY_CREDENTIALS, password); env.put(Context.SECURITY_AUTHENTICATION, ServiceCtx.NON_SSL_LOGIN); Context ic = new InitialContext (env); CustomerHome ch = (CustomerHome)ic.lookup (serviceURL + objectName); Customer cust = ch.create("Jake Terwilliger", "Pine Drive"); System.out.println("created " + cust.getName()); System.out.println (" address = " + cust.getAddress()); String pk = (String) cust.getPrimaryKey(); System.out.println("Primarykey = " + pk); //imagine that time passes here, or this program is //finished, and a later program wants to use the //primary key Customer cust1 = ch.create("Al Smith", "Sesame Street"); Customer cust2 = ch.create("Bob Davidson", "Elm Street"); Customer cust3 = ch.create("Carol Fernandez", "Cedar Blvd"); cust = null; cust = ch.findByPrimaryKey(pk); System.out.println("Found by primary key lookup"); System.out.println (" name = " + cust.getName()); System.out.println (" address = " + cust.getAddress()); cust.remove(); System.out.println("removed bean"); cust = ch.findByWhere("where cust_addr = 'Elm Street'"); System.out.println("Found by findByWhere"); System.out.println (" name = " + cust.getName()); System.out.println (" address = " + cust.getAddress()); cust.remove(); System.out.println("removed bean"); Enumeration e = ch.findAllCustomers(""); while(e.hasMoreElements()) { cust = (Customer) e.nextElement(); System.out.println (" name = " + cust.getName()); System.out.println (" address = " + cust.getAddress()); } } catch (RemoveException e) { System.out.println("RemoveException caught:" + e); e.printStackTrace(); } catch (NamingException e) { System.out.println("NamingException caught:" + e); e.printStackTrace(); } catch (FinderException e) { System.out.println("FinderException caught:" + e); e.printStackTrace(); } catch (CreateException e) { System.out.println("CreateException caught:" + e); e.printStackTrace(); } catch (RemoteException e) { System.out.println("RemoteException caught:" + e); e.printStackTrace(); } } }
package customer; import javax.ejb.EJBHome; import java.rmi.RemoteException; import javax.ejb.CreateException; import javax.ejb.FinderException; public interface CustomerHome extends EJBHome { public Customer findByPrimaryKey(String pk) throws RemoteException,
FinderException; public Customer findByWhere(String whereString) throws RemoteException,
FinderException; public java.util.Enumeration findAllCustomers(String whereString) throws
RemoteException, FinderException; public Customer create(String custname, String custaddr) throws
RemoteException, CreateException; }
package customer; import javax.ejb.EJBObject; import java.rmi.RemoteException; public interface Customer extends EJBObject { public String getName() throws RemoteException; public String getAddress() throws RemoteException; public void setAddress(String addr) throws RemoteException; }
package customerServer; import customer.*; import javax.ejb.*; import java.sql.*; import java.util.*; import java.rmi.RemoteException; import java.io.Serializable; public class CustomerBean implements EntityBean { private transient EntityContext ctx; public String name; public String addr; public String getName() throws RemoteException { return name; } public void setName(String name) throws RemoteException { this.name = name; } public String getAddress() throws RemoteException { return addr; } public void setAddress(String addr) throws RemoteException { this.addr = addr; } public void setEntityContext(EntityContext ctx) { this.ctx = ctx; Properties props = ctx.getEnvironment(); } public void unsetEntityContext() { this.ctx = null; } public String ejbCreate(String custname, String custaddr) throws CreateException, RemoteException { try { setName(custname); setAddress(custaddr); } catch (java.rmi.RemoteException e) { throw new CreateException(); } return null; } public String ejbFindByPrimaryKey(String pk) throws RemoteException, FinderException { return null; } public void ejbPostCreate(String custname, String custaddr) throws CreateException { // get primarykey String pk = (String)ctx.getPrimaryKey(); } public void ejbActivate() { } public void ejbPassivate() { } public void ejbRemove() { } public void ejbLoad() { // You can get to the primary key String pk = (String)ctx.getPrimaryKey(); } public void ejbStore() { } }
<?xml version="1.0"?> <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems Inc.//DTD Enterprise JavaBeans 1.1 //EN" "ejb-jar.dtd"> <ejb-jar> <enterprise-beans> <entity> <ejb-name>customerbean</ejb-name> <home>customer.CustomerHome</home> <remote>customer.Customer</remote> <ejb-class>customerServer.CustomerBean</ejb-class> <persistence-type>Container</persistence-type> <prim-key-class>java.lang.String</prim-key-class> <reentrant>False</reentrant> <cmp-field><field-name>name</field-name></cmp-field> <cmp-field><field-name>addr</field-name></cmp-field> <primkey-field>name</primkey-field> <env-entry> <env-entry-name>realmName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>my.realm</env-entry-value> </env-entry> <env-entry> <env-entry-name>customerBean.databaseURL</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>jdbc:oracle:kprb:</env-entry-value> </env-entry> <env-entry> <env-entry-name>customerBean.JDBCDriverName</env-entry-name> <env-entry-type>java.lang.String</env-entry-type> <env-entry-value>oracle.jdbc.driver.OracleDriver</env-entry-value> </env-entry> </entity> </enterprise-beans> <assembly-descriptor> <security-role> <role-name>PUBLIC</role-name> </security-role> <method-permission> <role-name>PUBLIC</role-name> <method> <ejb-name>test/customer</ejb-name> <method-name>*</method-name> </method> </method-permission> <container-transaction> <method> <ejb-name>test/customer</ejb-name> <method-name>*</method-name> </method> <trans-attribute>RequiresNew</trans-attribute> </container-transaction> </assembly-descriptor> </ejb-jar>
<?xml version="1.0"?> <!DOCTYPE oracle-descriptor PUBLIC "-//Oracle Corporation.//DTD Oracle 1.1//EN" "oracle-ejb-jar.dtd"> <oracle-descriptor> <mappings> <ejb-mapping> <ejb-name>customerbean</ejb-name> <jndi-name>test/customer2</jndi-name> </ejb-mapping> </mappings> <persistence-provider> <persistence-name>psi-ri</persistence-name> <persistence-deployer>oracle.aurora.ejb.persistence.ocmp.OcmpEntityDeployer
</persistence-deployer> </persistence-provider> <persistence-descriptor> <ejb-name>customerbean</ejb-name> <persistence-name>psi-ri</persistence-name> <psi-ri> <schema>SCOTT</schema> <table>customers</table> <attr-mapping> <field-name>name</field-name> <column-name>cust_name</column-name> </attr-mapping> <attr-mapping> <field-name>addr</field-name> <column-name>cust_addr</column-name> </attr-mapping> </psi-ri> </persistence-descriptor> </oracle-descriptor>
connect scott/tiger drop table CUSTOMERS; create table CUSTOMERS (CUST_NAME VARCHAR(64) NOT NULL, CUST_ADDR VARCHAR(64),
CUST_SERIALIZE LONG RAW );
|
![]() Copyright © 1996-2000, Oracle Corporation. All Rights Reserved. |
|