Java Reference
In-Depth Information
Session bean class
A stateless session bean is just a Java class annotated with the annotation
@Stateless
.
@Stateless(name = "EJB3SessionEJB",
mappedName = "EJB3RelationshipsJSF-Model-EJB3SessionEJB")
The
mappedName
element specifies the mapped name for the session bean. The
mappedName
is used in the JNDI lookup of the session bean from a client. The
@
Remote
annotation indicates that the class implements a remote interface:
@Remote
public class EJB3SessionEJBBean implements EJB3SessionEJB {
..
}
We shall be using an
EntityManager
for database persistence. Inject an
EntityManager
using dependency injection:
@PersistenceContext(unitName = "em")
private EntityManager em;
Add
getAll<>
methods to retrieve all
Edition
entities, all
Section
entities, and
all
Article
entities. By default, container-managed transactions do not require
individual methods to be associated with transactions. Methods may be associated
with transactions using the transaction attributes. Associate each of the
getAll
methods with a transaction attribute with
TransactionAttributeType
set to
REQUIRES_NEW
, which implies that a new transaction is required each time the
method is invoked:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
In each of the
getAll<>
methods, find all entities using the named query to find
all entities. The
getAll<>
methods return a parameterized
List
. For example, the
getAllEdtions
method returns
List<Edition>
. In the
getAllEditions
method,
create an
ArrayList<Edition>
type variable:
ArrayList<Edition> editions = new ArrayList<Edition>();
Create a
Query
object for the named query
findEditionsAll
, which was defined
in the
Edition
entity class. A
Query
object is created using the
createNamedQuery
method of the
EntityManager
object that was created using dependency injection:
Query q = em.createNamedQuery("findEditionsAll");
Search WWH ::
Custom Search