Getting Started: EJB Core Concepts

This topic will familiarize you with the basic concepts of EJB design and development.

What is an EJB?

EJBs are server-side components that are optimized to handle data and execute business tasks. There are three types of EJBs: entity beans, session beans, and message-driven beans. In this tutorial you will only learn about entity beans and session beans. Message-driven beans are discussed in the advanced Tutorial: Enterprise JavaBeans.

Entity beans model business objects. For instance, a Customer entity bean defines its properties and has methods to get and set the customer's name, age, and so forth. In addition, the Customer bean will likely have methods to create a new customer, delete a customer and find customers with certain properties. An entity bean ensures that the properties of a business object are mapped to a database table, so that these properties can be stored, updated and retrieved. What is interesting about using an entity bean is that you do not issue the SQL commands to interact with the database. The database-specific commands are entirely done in the background by the EJB container. This for instance means that to change the customer's age in the database, you only need to change this property, for instance, by calling the setAge(newAge) method on the Customer bean instance; the EJB container will ensure that the corresponding record in the database is updated to reflect this change.

Session beans model business logic and are often an intermediary between your client application, such as a web application, and entity beans. For instance, a Purchase session bean might take an 'add to shopping cart' request for a particular product through a web page, query an entity bean to find out whether this product is still in stock, and return a response to the web application that the product is not available, or is backordered, or is available and has been successfully added to the cart. There are two types of session beans, stateless and stateful. A stateful session bean maintains conversational state. In other words, a stateful session bean remembers the calling client application from one method to the next. Stateless session beans do not maintain conversational state. Stateless session beans are more commonly used and will be addressed in this tutorial.

All EJBs must adhere to a certain set of design specifications (as set by the Java Community Process). For instance, all session and entity beans must have a home interface containing methods to obtain a reference to a (new or existing) bean instance. These beans must also have a business interface containing the methods to get or set data, and execute business logic. These interfaces can be local, meaning that only other objects in the same application can call these methods, remote, meaning that objects in the same and other applications can call these methods, or both.

According to the EJB design specifications, the definitions of the bean class and the various interfaces need be stored in separate Java files (and the corresponding compiled .class files), which means that a developer needs to keep the various files and definitions synchronized. In WebLogic Workshop you use only one file to develop the bean class and interfaces. When you build this file, the various Java files for the bean class and the interfaces are automatically generated for you, releasing you of the task to keep this information synchronized.

Developing EJBs with WebLogic Workshop

EJBs are developed using WebLogic Workshop, a visual tool for designing J2EE applications. The image below shows the finished tutorial.

The Application tab (area 1) shows the application's source files. Enterprise JavaBeans are stored as .ejb files, which is a special Java file with an .ejb extension.

The main work area (area 2) shows the Design and Source View of the components you are building. In the above picture an entity bean is shown in Design View. You can develop EJBs in Design View with the assistance of dialogs and wizards and/or you can add source code directly to the Source View.

The Property Editor (area 3) allows you to see and set properties of the EJB shown in the main area as well as properties of the EJB project.

An EJB project folder holds EJBs that are developed in an application. An EJB project is like an organizational unit; when building EJBs you build all the EJB in a project and the resulting files are wrapped in a JAR file with the same name as the EJB project. You can have one or multiple EJB projects in an application. In this tutorial you will develop all EJBs using one EJB Project folder called MyEJBProject (area 4).

Other components of the application are built in other projects. In this tutorial you will build a test web service, which you will use as a client application to test the EJBs. Web services are created in a web service project folder, called MyTestProject in this tutorial (area 5).

Related Topics

Getting Started with EJB Project

Click the arrow to navigate to the next step.