|
EJB Tutorial
| ||
Tutorial Index Page
Using the Entity BeanLook in the "Props.java" file created by the wizard. This is your "remote" interface. Recall that the "remote" interface is the way how anybody interacts with the entity bean.Of course, before interacting with the entity bean, one must find it first! Unless one is creating a new entity bean. The interface that allows clients to create and find entity beans, is in PropsHome.java. Take a look in this file to see what this "home" interface looks like. Getting the home interfaceThe first step in using an entity bean is to get a reference to the home object. This is done via JNDI, as shown:import javax.naming.*; import javax.ejb.*; import javax.rmi.*; import java.util.*; import props.*; // ... Properties env = new Properties(); env.put( "java.naming.factory.initial", "desisoft.ejb.client.JRMPFactory" ); env.put( "desisoft.ejb.nameServer1", "localhost:2050" ); Context ctx = new InitialContext( env ); PropsHome home = (PropsHome) ctx.lookup( "Props" );Notes:
Using the entity beanOnce you have a home interface, creating or finding a bean is simple, just call the appropriate create or find method from the home interface.When you create or find a bean, you get the bean's "remote" interface back. To use the bean, now just call methods in the bean's remote interface. The client codeThere is a sample file included here to show how to put all this together. There are two versions of the file, EntityClientSample.java for JDK 1.2 and EntityClientSample.java for JDK 1.3. The only difference between these two versions is that the JDK 1.2 version does not use PortableRemoteObject.To compile and use this file, you need to add the EJB jar file C:\Ejbtut\PropsEjb.jar in your classpath. (Do not place the client code in C:\Ejbtut to avoid confusion with the existing classes there!) Usage of this class is shown below: java EntityClientSample put name myname java EntityClientSample get name java EntityClientSample put name anotherName java EntityClientSample get name(The EJB server must be running when you try these commands.) The first command above puts the value "myname" on the property "name". The second command retrieves the value on the property "name". The third command changes the property, and the fourth command retrieves the new property. You can put a property on one machine, and then access it from another machine! And once you put the property, even when you stop the program and turn your machine off the property will stay that way, so when you restart the program and lookup the property you will get the same value back. You can even stop and restart the server, and entity bean object will be there when the server is restarted. ConceptsThe entity bean created has two fields, "key" and "value". The EJB framework guarantees that the fields of any given bean will be persistent, and will stay the same until changed.This sample provides a very simple entity bean where the fields are returned un-changed to the client of the bean, and the only bean methods are getters and setters. But entity beans can provide any other methods besides getters and setters. While the underlying fields will be persistent, any necessary computation can be performed on the fields before returning a result to the client. Exercise: Write a client program for the employees EJB you have created. This program should be able to (a) add new employees, given their SSN and other information (b) retrieve and print information about an employee, given the SSN, (c) change the information for an employee, given the SSN and the new information (restrict "c" to only one field, e.g. salary, the idea is just to make sure you are familiar with the capabilities.)
| ||
|