Feeds

Migrating EJB 2.1 Entity and Session Beans to EJB 3.0

The nuts and bolts

Choosing a cloud hosting partner with confidence

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);
    }
}

Intelligent flash storage arrays

Next page: Summary

More from The Register

next story
Netscape Navigator - the browser that started it all - turns 20
It was 20 years ago today, Marc Andreeesen taught the band to play
Sway: Microsoft's new Office app doesn't have an Undo function
Content aggregation, meet the workplace ... oh
Sign off my IT project or I’ll PHONE your MUM
Honestly, it’s a piece of piss
Return of the Jedi – Apache reclaims web server crown
.london, .hamburg and .公司 - that's .com in Chinese - storm the web server charts
NetWare sales revive in China thanks to that man Snowden
If it ain't Microsoft, it's in fashion behind the Great Firewall
Chrome 38's new HTML tag support makes fatties FIT and SKINNIER
First browser to protect networks' bandwith using official spec
Admins! Never mind POODLE, there're NEW OpenSSL bugs to splat
Four new patches for open-source crypto libraries
prev story

Whitepapers

Forging a new future with identity relationship management
Learn about ForgeRock's next generation IRM platform and how it is designed to empower CEOS's and enterprises to engage with consumers.
Cloud and hybrid-cloud data protection for VMware
Learn how quick and easy it is to configure backups and perform restores for VMware environments.
Three 1TB solid state scorchers up for grabs
Big SSDs can be expensive but think big and think free because you could be the lucky winner of one of three 1TB Samsung SSD 840 EVO drives that we’re giving away worth over £300 apiece.
Reg Reader Research: SaaS based Email and Office Productivity Tools
Read this Reg reader report which provides advice and guidance for SMBs towards the use of SaaS based email and Office productivity tools.
Security for virtualized datacentres
Legacy security solutions are inefficient due to the architectural differences between physical and virtual environments.