Feeds

Migrating EJB 2.1 Entity and Session Beans to EJB 3.0

The nuts and bolts

The Power of One Brief: Top reasons to choose HP BladeSystem

Table 1. EJB 3.0 Metadata Annotations

Annotation Description Annotation Elements
@Entity Specifies an entity bean.
@Table Specifies the entity bean table. name, schema
@Id Specifies an identifier property.
@Column Specifies the database table column for a persistent entity bean property. name, primaryKey, nullable, length
@NamedQueries Specifies a group of named queries.
@NamedQuery Specifies a named query or a query associated with a finder method. name, queryString
@OneToMany Specifies a one-to-many CMR relationship. cascade
@OneToOne Specifies a one-to-one CMR relationship. cascade
@ManyToMany Specifies a many-to-many CMR relationship. cascade
@ManyToOne Specifies a many-to-one CMR relationship. cascade

The EJB 3.0 annotation types are defined in the javax.persistence package. The EJB 3.0 entity bean class, corresponding to the EJB 2.1 entity bean class, is annotated with metadata annotation @Entity. The finder method findByJournal in the EJB 2.1 bean class is specified in the EJB 3.0 POJO class with the @NamedQuery annotation. The CMR relationship Catalog-Editions in the EJB 2.1 entity bean is specified in EJB 3.0 entity bean class with the @OneToMany annotation. The @Id annotation specifies the identifier property catalogId. The @Column annotation specifies the database column corresponding to the identifier property catalogId. If a @Column annotation is not specified for a persistent entity bean property, the column name is same as the entity bean property name. Transient entity bean properties are specified with the @Transient annotation. The EJB 3.0 entity bean POJO class, corresponding to the EJB 2.1 entity bean is listed in Listing 8.

Listing 8. CatalogBean.java. EJB 3.0 Entity Bean POJO Class

import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import javax.persistence.Id;
import javax.persistence.Column;
import javax.persistence.OneToMany;

@Entity
@NamedQuery(name="findByJournal",
    queryString="SELECT DISTINCT OBJECT(obj)  FROM Catalog obj WHERE obj.journal = ?1")

public class CatalogBean{
    public CatalogBean() {}
    public CatalogBean(String catalogId) {
        this.catalogId=catalogId;
    }

    private String catalogId;
    private String journal;
    private String publisher;

    @Id
    @Column(name="CatalogId", primaryKey="true")
    public  String getCatalogId() {return catalogId;}
    public  void setCatalogId() {this.catalogId=catalogId;}

    public  void setJournal(String journal) {this.journal=journal;}
    public String getJournal() {return journal;}

    public  void setPublisher(String publisher) {this.publisher=publisher;}
    public  String getPublisher() {return publisher;}

    private java.util.Collection<Edition> editions;

    @OneToMany
    public  void setEditions(java.util.Collection editions) {
        this.editions=editions;
    }

    public  java.util.Collection getEditions() {return editions;}
}

An EJB 2.1 entity bean is created with the create() method in the entity bean home/local home interface. A client for an EJB 2.1 entity bean obtains a reference for the entity bean with JNDI lookup. An example code snippet to create an instance of the example EJB 2.1 entity bean is:

InitialContext ctx=new InitialContext();
Object objref=ctx.lookup("CatalogLocalHome");
CatalogLocalHome catalogLocalHome=(CatalogLocalHome)objref;

//Create an instance of Entity bean
CatalogLocal catalogLocal=(CatalogLocal)catalogLocalHome.create(catalogId);

The example client class for the EJB 2.1 entity bean class is available in the resources zip file . CatalogLocalHome is the JNDI name of the CatalogBean entity bean.

To access the getter/setter methods of an entity bean, the remote/local object in EJB 2.1 is obtained with the finder methods:

CatalogLocal catalogLocal =
    (CatalogLocal) catalogLocalHome.findByPrimaryKey(catalogId);

A entity bean instance is removed with the remove() method:

catalogLocal.remove();

The example client class for the EJB 2.1 entity bean class is available in the resources zip file.

An EJB 3.0 entity bean class does not include the local/remote and home/local home interfaces. In EJB 3.0, persistence and lookup is provided by the EntityMangerclass.

Some of the methods in the javax.persistence.EntityManager class are listed in Table 2.

Seven Steps to Software Security

More from The Register

next story
KDE releases ice-cream coloured Plasma 5 just in time for summer
Melty but refreshing - popular rival to Mint's Cinnamon's still a work in progress
NO MORE ALL CAPS and other pleasures of Visual Studio 14
Unpicking a packed preview that breaks down ASP.NET
Secure microkernel that uses maths to be 'bug free' goes open source
Hacker-repelling, drone-protecting code will soon be yours to tweak as you see fit
Cheer up, Nokia fans. It can start making mobes again in 18 months
The real winner of the Nokia sale is *drumroll* ... Nokia
Put down that Oracle database patch: It could cost $23,000 per CPU
On-by-default INMEMORY tech a boon for developers ... as long as they can afford it
Another day, another Firefox: Version 31 is upon us ALREADY
Web devs, Mozilla really wants you to like this one
Google shows off new Chrome OS look
Athena springs full-grown from Chromium project's head
prev story

Whitepapers

Implementing global e-invoicing with guaranteed legal certainty
Explaining the role local tax compliance plays in successful supply chain management and e-business and how leading global brands are addressing this.
Consolidation: The Foundation for IT Business Transformation
In this whitepaper learn how effective consolidation of IT and business resources can enable multiple, meaningful business benefits.
Application security programs and practises
Follow a few strategies and your organization can gain the full benefits of open source and the cloud without compromising the security of your applications.
How modern custom applications can spur business growth
Learn how to create, deploy and manage custom applications without consuming or expanding the need for scarce, expensive IT resources.
Securing Web Applications Made Simple and Scalable
Learn how automated security testing can provide a simple and scalable way to protect your web applications.