Java Reference
In-Depth Information
The session bean class
The
CatalogSessionEJBBean
class is annotated with the annotation
@Stateless
.
The
mappedName
attribute specifies the global JNDI for the session bean. We shall
use the mapped name in the web service to look up the session bean and invoke
method/s on it. The
@Remote
annotation indicates that the session bean is a remote
interface:
@Stateless(name = "CatalogSessionEJB", mappedName = "EJB3-SessionEJB")
@Remote
public class CatalogSessionEJBBean implements CatalogSessionEJBRemote
{ }
Two types of
EntityManager
s are supported by the JPA: application-managed
EntityManager
and container-managed
EntityManager
.
Container-managed
EntityManager
s always use JTA transactions, which
are managed by the EJB container and are created by injecting using the
@
PersistenceContext
annotation or by direct lookup of the
entity
manager
in the
JNDI namespace.
Application-managed entity managers may use JTA or resource-local
transactions. Application-managed entity managers are created by injecting an
EntityManagedFactory
with the
@PersistenceUnit
annotation and subsequently
invoking the
createEntityManager()
method on the
EntityManagedFactory
object.
We are using a container-managed entity manager. In the session bean, inject an
EntityManager
using the
@PersistenceContext
annotation. One of the value-added
features of WebLogic Server 10.3 is that if the injected variable's name is the same
as the persistence unit, the
unitName
attribute of the
@PersistenceContext
or
@PersistenceUnit
is not required to be specified, though we have specified it. The
type attribute is set to
PersistenceContextType.TRANSACTION
, which implies that
the persistence context is transaction-scoped.
@PersistenceContext(unitName = "em",
type = PersistenceContextType.TRANSACTION)
EntityManager em;
Add a method
persistEntity()
and a method
test()
to the session bean and the
remote interface. The
persistEntity
method is used to persist an entity using the
persist()
method. Subsequent to persisting an entity instance, invoke the
flush()
method to synchronize the entity manager with the database:
em.persist(entity);
em.flush();
Search WWH ::
Custom Search