Entity Beans  «Prev  Next»

Lesson 4Using and removing the bean instance
ObjectiveWrite Code to access Business Methods of the Bean and then remove it

Write Code to access Business Methods of the Bean and then remove it

Accessing the business methods

As previously mentioned, the remote reference is returned from:
  1. findByPrimaryKey()
  2. create()

Using this reference, the business methods of the bean can be invoked in exactly the same way as session beans. In the code extract below, once the instance and the data are created, the setName() and the getInfo() methods can be called as follows:

Customer mickey = home.create("111");
mickey.setName("mickey");
System.out.println("Created " + mickey.getInfo());

Removing an entity bean

It is important to understand that when you remove the bean instance, the underlying entity object in the persistent store also is removed. You must be careful, as this is very different from the situation for session beans. There are three ways to remove an entity bean instance. From the EJBObject, assuming you have a remote reference, you can use the method:
  1. void remove()

From EJBHome via the home object you can use the methods:
  1. void remove(java.lang.Object primaryKey)
  2. void removeHandle(Handle)

To use these methods, the client program does not need the remote reference. It only needs the remote reference to the home object, which is available from JNDI. However, you do need the primary key. Here is an example from the Customer client that uses the primary key for Customer bean:

Customer mickey = home.create("111");
mickey.setName("mickey");
System.out.println("Created " + mickey.getInfo());
mickey.remove("111");

The next lesson introduces the identity of entity beans.