Feeds

Migrating EJB 2.1 Entity and Session Beans to EJB 3.0

The nuts and bolts

Top 5 reasons to deploy VMware with Tegile

Table 2. EntityManager Class

EntityManager Method Description
persist Creates an entity bean instance.
createNamedQuery Creates a named query.
find Finds an entity bean instance.
createQuery Creates an EJBQL query.
remove Removes an entity bean instance.

In a session bean client class for EJB 3.0 entity bean, the EntityManager reference is obtained with the @Resource annotation:

@Resource
private EntityManager em;

An entity bean instance is created with the persist() method of the EntityManager class:

CatalogBean catalogBean=new CatalogBean(catalogId);
em.persist(catalogBean);

An entity bean instance is obtained with the find() method:

CatalogBean catalogBean=(CatalogBean)em.find("CatalogBean", catalogId);

A finder method may be defined corresponding to the named query findByJournal in the entity bean POJO class. In the finder method a Query object is obtained with the createNamedQuery method:

Query query=em.createNamedQuery("findByJournal");

Set the Query object parameters with the setParameter method:

query.setParameter(0, journal);

Obtain a Collection of the CatalogBean with the getResultList() method. If a Query object returns a single result, the getSingleResult() is used:

java.util.Collection catalogBeanCollection=(CatalogBean)query.getResultList();

An entity bean instance is removed with the remove() method of the EntityManager class:

CatalogBean catalogBean;
em.remove(catalogBean);

The client class for the EJB 3.0 entity bean is listed in Listing 9.

Listing 9. CatalogClient. EJB 3.0 Client Class

import javax.ejb.Stateless;
import javax.ejb.Resource;
import javax.persistence.EntityManager;
import javax.persistence.Query;

@Stateless
@Local
public class CatalogClient implements CatalogLocal {
    @Resource
    private EntityManager em;

    public void create(String catalogId) {
        CatalogBean catalogBean=new CatalogBean(catalogId);
        em.persist(catalogBean);
    }

    public CatalogBean findByPrimaryKey(String catalogId) {
        return (CatalogBean)em.find("CatalogBean", catalogId);
    }

    public java.util.Collection findByJournal(String journal) {
        Query query=em.createNamedQuery("findByJournal");
        query.setParameter(0, journal);
        return (CatalogBean)query.getResultList();
    }

    public void remove(CatalogBean catalogBean) {
        em.remove(catalogBean);
    }
}

Beginner's guide to SSL certificates

Next page: Summary

More from The Register

next story
Euro Parliament VOTES to BREAK UP GOOGLE. Er, OK then
It CANNA do it, captain.They DON'T have the POWER!
Download alert: Nearly ALL top 100 Android, iOS paid apps hacked
Attack of the Clones? Yeah, but much, much scarier – report
NSA SOURCE CODE LEAK: Information slurp tools to appear online
Now you can run your own intelligence agency
Post-Microsoft, post-PC programming: The portable REVOLUTION
Code jockeys: count up and grab your fabulous tablets
Twitter App Graph exposes smartphone spyware feature
You don't want everyone to compile app lists from your fondleware? BAD LUCK
Microsoft adds video offering to Office 365. Oh NOES, you'll need Adobe Flash
Lovely presentations... but not on your Flash-hating mobe
prev story

Whitepapers

Designing and building an open ITOA architecture
Learn about a new IT data taxonomy defined by the four data sources of IT visibility: wire, machine, agent, and synthetic data sets.
Getting started with customer-focused identity management
Learn why identity is a fundamental requirement to digital growth, and how without it there is no way to identify and engage customers in a meaningful way.
5 critical considerations for enterprise cloud backup
Key considerations when evaluating cloud backup solutions to ensure adequate protection security and availability of enterprise data.
High Performance for All
While HPC is not new, it has traditionally been seen as a specialist area – is it now geared up to meet more mainstream requirements?
Driving business with continuous operational intelligence
Introducing an innovative approach offered by ExtraHop for producing continuous operational intelligence.