Java Reference
In-Depth Information
In the
deleteSomeData()
method, we shall delete some data. For example, we shall
delete the magazine catalog for
Oracle
Magazine
. First we create a
Query
object to
find a
Catalog
entity instance by journal name:
Query q = em.createNamedQuery("findCatalogByJournal");
Set the journal name to
Oracle Magazine
using the
setParameter()
method:
q.setParameter("journal", "Oracle Magazine");
Get a
List
of
Catalog
s for the specified query using the
getResultList()
method:
List list = q.getResultList();
Iterate over the
List
and remove the
Catalog
entities using the
remove()
method:
for (Object catalog : list) {
em.remove(catalog);
}
The
CatalogSessionEJBBean
class is listed next:
package model;
import javax.ejb.*;
import javax.persistence.*;
import java.util.*;
@Stateless(name = "CatalogSessionEJB", mappedName = "EJB3-SessionEJB")
public class CatalogSessionEJBBean implements CatalogSessionEJBRemote
{
First, inject an
EntityManager
:
@PersistenceContext(unitName = "em")
EntityManager em;
Define the getter methods:
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Edition> getAllEditions() {
ArrayList<Edition> editions = new ArrayList<Edition>();
Query q = em.createNamedQuery("findEditionAll");
for (Object ed : q.getResultList()) {
editions.add((Edition)ed);
}
return editions;
}
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public List<Section> getAllSections() {
Search WWH ::
Custom Search