Adding Business Logic to an Enterprise Bean


In this section, you learn about the different method types you can add to an enterprise bean. Remember that NetBeans IDE provides wizards that greatly simplify the work of coding business logic within enterprise beans.

An enterprise application gets its work done through methods that the client calls on the bean. The following method types are either automatically generated via the wizard with the default implementation or can be added via popup menu actions in the Projects window or the Source Editor window for an enterprise bean implementation class. The IDE takes care of necessary extra generation (such as updating the local or remote interfaces with the correct signature entry).

  • Business methods. A client calls business methods on a bean through the bean's remote interface (or local interface, as applicable).

    You have to add business methods to the bean yourself; the IDE doesn't generate any default business method declarations. However, when you specify a business method, the IDE places matching method declarations in the bean class and in the remote, local, or remote and local interfaces.

    Business methods are the most important methods for an enterprise bean. These are the ones called by other enterprise beans or web tier components, like JSP files or servlets. A special IDE wizard is available to simplify the coding of calling an EJB business method. From a servlet or enterprise bean file in the Source Editor, right-click to activate the popup menu, and choose Enterprise Resources | Call Enterprise Bean.


  • Life-cycle methods. The container calls several methods to manage the life cycle of an enterprise bean. Depending on the type of bean, the container works through the methods in slightly different ways. You have the option of specifying parameters for some of these methods.

    The IDE automatically generates the appropriate life-cycle method declarations for each type of bean and places them in the bean class.

  • Finder methods. The client goes through the home interface to find an entity bean instance by its primary key. You can also add other finder methods.

    The IDE automatically generates a findByPrimaryKey method declaration in the local home interface of every entity bean (and in the bean's home interface, if it has one). The IDE also places a corresponding ejbFindByPrimaryKey method declaration in the bean class of every entity bean that manages its own persistence (that is, a bean-managed persistent entity bean, or BMP entity bean). If you add another finder method, the IDE automatically places the corresponding method declarations in the local home (and home) interface and, for BMP entity beans, in the bean class.

    An entity bean that delegates its persistence to the container is called a container-managed persistent entity bean, or CMP entity bean. Finder methods that are added to CMP entity beans include EJB Query Language (EJB QL) statements, which are converted automatically to the kind of SQL code the server needs.

  • Create methods. The container initializes the enterprise bean instance, using the create method's arguments.

  • Home methods. An entity bean can use a home method for a lightweight operation that doesn't require access to any particular instance of the bean. (By contrast, a business method does require access to a particular instance.) It is up to you to add the home method explicitly; the IDE generates the corresponding method declaration in the bean class and the bean's local home or home interface. An entity bean can have any number of home methods.

  • Select methods. A CMP entity bean can use a select method. Like a finder method, a select method can query the database and return a local or remote interface or a collection. In addition, a select method can query a related entity bean within the same EJB module and return values from its persistent fields. Select methods aren't exposed in remote-type interfaces and can't be invoked by a client.

  • onMessage methods. A client sends a message through a Java Message Service (JMS) destination to call an onMessage method on a message-driven bean.



NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
NetBeans IDE Field Guide(c) Developing Desktop, Web, Enterprise, and Mobile Applications
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 279

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net