What do we have in this session 5?
|
Creating the Session Façade
In this exercise you will use a wizard to create a stateless session facade for the Message entity. The EJB 3.1. specification states that business interfaces for session beans are now optional. In this application where the client accessing the bean is a local client, you have the option to use a local interface or a no-interface view to expose the bean. To create the session bean, perform the following steps.
Right-click the project node and choose New > Other.
|
Select Session Beans for Entity Classes from the Java EE category. Click Next.
Select the Message entity (entities.Message) and click Add. Click Next.
------------------------------------------------------------------------------------------------------------------------------------
Type boundary for the package. Click Finish.
Notice that you did not need to create a business interface for the session bean. Instead, in this application the bean will be exposed to a local managed bean using a no-interface view.
When you click Finish, the session facade class MessageFacade.java is created and opens in the Source Editor. The bean class contains the business logic and manages the EntityManager. As you can see in the generated code, the annotation @Stateless is used to declare the class as a stateless session bean component.
In NetBeans IDE 6.9, the wizard generates an AbstractFacade class that contains the business logic and the facade class for the entity extends AbstractFacade as shown below.
@Stateless
public class MessageFacade {
@PersistenceContext(unitName = "SimpleEE6AppPU")
private EntityManager em;
When you create the facade for the entity using the wizard, by default the IDE adds the PersistenceContext annotation (@PersistenceContext(unitName = "SimpleEE6AppPU")) to inject the entity manager resource into the session bean component and to specify the name of the persistence unit. In this example the name of the persistence unit is declared explicitly, but the name is optional if the application has only one persistence unit.
The IDE also generates methods in the facade to create, edit, remove and find entities. The EntityManager API defines the methods that are used to interact with the persistence context. You can see that IDE generates some commonly used default query methods that can be used to find entity objects.
-------------------------------------------------------------------
The findAll, findRange and count methods use methods defined in the Criteria API for creating queries. The Criteria API is part of the JPA 2.0 specification that is included in the Java EE 6 specification.
Don’t forget to rectify the warning encountered in MessageFacade.java file as shown below (if any).
Press Alt-Enter or click the yellow bulb on the line number to show the hint. Select the given hint.