Book Image

EJB 3 Developer Guide

By : Michael Sikora
Book Image

EJB 3 Developer Guide

By: Michael Sikora

Overview of this book

Table of Contents (18 chapters)
EJB 3 Developer Guide
Credits
About the Author
About the Reviewers
Preface
Annotations and Their Corresponding Packages

Entity Manager Methods


In this section we look at some more EntityManager methods.

remove()

The remove() method flags an entity for removal from the database. Note that the entity must be a managed entity. The removal need not take place immediately. Removal occurs whenever the EntityManager decides to synchronize, or flush, the persistence context with the database. Typically this occurs when a transaction is committed. The code below shows a BankServiceBean method, removeCustomer(), which would be invoked in order to remove a Customer entity.

public void removeCustomer(Customer cust) {
Customer mergedCust = em.merge(cust);
em.remove(mergedCust);
}

Note that the method first merges an entity to ensure it is managed before removing it.

contains()

The contains() method is used to check whether an entity is managed in the current persistence context. It returns a boolean which is set to true if the entity is managed, otherwise it set to false. For example:

if (em.contains(cust)) {
System.out...