The following two sections describe how to write Enterprise Beans from five aspects:
First, Enterprise Javabean's customer view:
Enterprise JavaBean customers are a separate application, or a servlet, or an applet, still or another Enterprise Javabean. In any case, customers must use an Enterprise JavaBean to do the following:
First, you want JavaBean's Home interface: The EJB specification requires customers to locate the Bean's Home interface using Java Naming and Direcotry Interface API.
Then get a reference to the Remote interface of Enterprise JavaBean: You can use the method defined in the Home interface of Enterprise Beans. A session bean can also be generated or looking for (find) an entity bean.
Call the method defined in Enterprise JavaBean: The customer does not directly call the method defined in Enterprise JavaBean. Instead, the method in the JavaBean class is used to expose to the user in the Remote interface of Enterprise JavaBean. The method defined in the Remote Interface is to expose to the user in JavaBean, allowing users to use methods.
Initialize customers:
In the example of the IAS, the necessary JNDI class is introduced as a customer's SortClient application, and the HOME interface of the SortBean and the REMOTE interface are also introduced. Customers use JNDI API to locate the Home interface of Enterprise Javabean.
Positioning home interface:
The client is positioned with JNDI. First, you need to obtain a JNDI initialization context Context. The following code instantiates a new javax.naming.context class. This is called InitialContext in our example. The customer can then use the context's lookup () method to correspond to the Home interface from one name. Note that the initialization of the initial name context is specified by the EJB container / service.
The context's lookup () method returns an object of java.lang.Object. The code must convert the returned object to the desired type. The following code is a piece of code in the SortClient example. Main () starts from using JNDI services and locates the Home interface with the cached lookup () method. Pass the name of the Remote interface (in this example is Sort) to the Context.loopup () method. Note that the program finally passes the results of the context.lookup () method to the SortHome, the home interface.
Position the home interface with JNDI:
// SortClient Java
Import javax.naming.initialcontext;
Import sorthome; // Introduced bean home interface
Import sort; // Introduced the Remote interface of Bean
Public class sortclient {
......
Public static void main (string [] args) Throws Exception
Javax.naming.context context;
{// Get JNDI context with naming service
Context = new.javax.naming.initialcontext ();
}
Object objref = context.lookup (sort);
Sorthome Home = (Sorthome) javax.rmi.portableremoteObject.narrow (Objref,
Sorthome class;
Sort sort = home.create ();
// Do sort work
Sort remove ();
}
MAINT () first throws an ordinary exception ExcePion. When the code is executed here, even if the customer may lead to the termination of the program, customers also need to capture any exceptions.
Get the REMOTE interface:
Now we get the Home interface of Enterprise Javabean, then use the Create () method of the HOME interface, or the Finder () method you can get its Remote interface. What way actually uses depends on the type of Enterprise JavaBean and the provider of Enterprise JavaBean in the HOME interface definition.
For example: The above code shows how the customer's SortClient gets references to the REMOTE interface of the Sort. Once SortClient gets the reference to the HOME interface and converts it to the appropriate type (here is SortHome), the customer can generate the bean instance and call it. In this example, the Create () method of the HOME interface is called, and the method returns a reference to the Remote interface. (Because SortBean is a stateless session bean in this example, its HOME interface has only one create () method, this method does not have any parameters). Then, SortClient can call the method defined in the Remote interface: sort () and merge () to sort work. When the sorting work is completed, the client calls the Remote interface REMOVE method to delete an instance of Enterprise Bean.
Session bean:
The client gets references by calling the CREATE () method in the Home interface.
Each session bean must have at least one Create () method. The stateless session bean can only have a create () method, and this CREATE () method does not have parameters. There is a CREATE () method without parameters, or a CREATE () method with different parameters. The parameters in the Create () method are used to initialize the session bean.
The default CREATE () method has no parameters. For example, an Sort example uses a stateless session bean. According to the definition, it has a CREATE () method without parameters.
Sort sort = home.create ();
We compare another example: Cart example, use a stateful session bean. A number of CREATE () methods are implemented in its HOME interface. One of the CREATE () methods has three parameters, three parameters used to identify CART purchases, returns a reference to the HOME interface of Cart. CART customers first give the values of the three parameters to the three variables of CARTHOLDNAME, CREDITCARTNUMBER, and EXPIRATIONDATE, and then call the create () method. The code is as follows:
How to call the code of the CREATE () method:
Cart Cart;
{
String cartholdername = "Jack B.quick" string creditcartnumber = "345-346-32525252"
Date expirationdate = new Gregoriancalendar (2001, Calendar July, 1) gettime ();
Cart = Home.create (CARTHOLDERNAME, CREDITCARTNUMBER, EXPIRATIONDATE);
}
Also, pay attention is that the session bean does not have a Finder method.
Entity Bean:
With the Finder method, or by the CREATE method, the customer gets references to entity objects. We mentioned earlier that entity objects represent some data stored in the database. Since the entity bean represents some lasting data, usually there is a relatively long period of time, certainly more than the customer calls it, so that customers usually need to find the data representative of the database that is interested in the database. Entity bean, not create an entity bean. The Create operation will produce a new data and the new data is present in the underlying database.
The client is operated by Find operations that already exist entssure beans. Can be the row specified in the relevant database. That is, the Finder operation is positioned a data entity that has been inserted into the database. This data may have been added to the database by the entity bean, perhaps the external part of the EJB context (eg, a database management system DBMS). Or in the original system, this data has existed before installing EJB.
The customer uses the CREATE method to generate a new data entity and store the data entity in the underlying database. The CREATE method of the entity bean plugs the entity data into the database, and initializes the variables of the entity with the parameter of the CREATE method. The CREATE method of the entity bean always returns a Remote interface. However, the corresponding EJBCREATE () method is the primary key to returning the entity bean instance.
Every entity bean must have a main key value to uniquely identify it. An entity bean can also have a key value to locate a specific entity object.
Find methods and primary keyts:
The default Find method of an entity bean is a FindByPrimaryKey () method. It locates the entity bean by primary key. The syntax is as follows:
Each entity bean must implement a FindByPrimaryKey method. The PrimaryKey parameter is an independent key class that defines in the Configuration Descriptor. The type of key is the primary key, and must be a legal value of the RMI-IIOP. The key class can be any Java class or a class you write.
For example, you may have an entity bean named ACOUNT. It has been defined as Accountpk. Accountpk is a string type. It has an identification of Account Bean. We set the identification of the Account Bean to AccountPK, then call the FindByPrimaryKey method, you can get a reference to the Account Bean instance. The following code shows:
Finding a Entity Bean Instance Using The Primary Key
Accountpk Accountpk = New Accountpk ("1234-56-789");
Account Source = AccountHome.FindByPrimaryKey (Accountpk);
BEAN's providers can define additional Finder methods to use customers.
Create () method and remove () method: The customer can also create an entity bean with the CREATE () method defined in the HOME interface. When the customer calls the Create () method of the entity bean, a new entity bean instance is sent to the data warehouse. The new instance always has a primary key to identify it. Its state can be initialized by the parameter value passed by the CREATE method.
Note: The time exists in the entity bean is the same as the data represented in the database. Its life is not determined by a session with the customer. By calling the Remove method of the entity bean, the entity bean can be deleted. The REMOVE method not only deletes entity beans, but also deletes the data represented from the database. Alternatively, the entity bean can be directly deleted. For example, it can be deleted by using the original application or a method of recording a record in the database with DBMS.
Call method:
Once the customer gets a reference to the Remote interface of the bean. You can call the method defined in the Remote Interface for Enterprise Bean. The method of application logic belonging to this bean is to be interested in customers. There are also some ways to use information or interface information for enteterprise beans. For example: Handle of the bean object, check if a bean is the same as another bean, and deletes the bean method.
The following code explains how customers call methods in Enterprise Beans. This example calls Cart this session bean. Code starts from generating a new CART bean instance and re-found a recaps of a CART bean instance. From here, customers are ready to call beans.
First, the customer created a new Book object, set its Title and Price parameters. Then, the application method of Enterprise Bean is called AddItem () to add a Book object to the Shopping Cart. The addItem () method is defined in the CartBean session bean, turning it into a public approach through the REMOTE interface of Cart. Customers add another thing and then use the summairze () method to list all Items in Shopping Cart. Finally, the REMOVE method is called to delete the instance of the bean. Note that customers use other methods to pass the same way, even if they define the method Summarize ().
Method for calling bean
.........
Cart Cart;
{
...
// Get instances of the Remote interface of the bean.
Cart = Home.create (CARTHODlerName, CreditCardNumber, ExpirationDate);
}
// Create a new Book object.
Book knuthbook = New book ("The Art of Computer Programming", 49.95F);
// Put the new book to CART.
Cart.Additem (knutbook);
.........
// list the book in Cart in Cart
Summarize (CART);
Cart.RemoveItem (Knutbook);
......
Remove the instance of bean:
The REMOVE method is different for the operation of the session bean and the operation of the entity bean. Because for customers, the existence of session objects is not lasting. The client of the session object needs to call the Remove method after completing the session. For customers, there are two remove methods to call: You can use the javax.ejb.ejbobject.remove () method to delete the session bean; you can also use the javax.ejb.ebhome.remove (Handle Handle) method to delete session beans. Handle.
A good programming style is not needed to delete a session bean object by the customer. If the customer does not delete a stateful session bean, the container will automatically delete this object after passing the time (specified timeout value timeout value). Timeout value is a configuration properties. However, customers can keep this object's handle for later references. Entity Beans don't need to consider this problem because entity beans are just during the customer's transaction. The container is responsible for its life cycle, including activation and passivation. Just call the Remove method when the customer needs to delete the entity object from the underlying database.