Magazine
 
Working with Entity bean using JPA

container for an instance of the session bean to use, either through dependency injection or, for external components, through a JNDI lookup. The class is tagged with the @Stateless annotation, which tells the container that the bean object does not maintain any client state
information between method invocations. The caller component gets a fresh and random BookCatalogBean instance every time when it makes a bean method call.

In order to use the entity beans in the session bean, you need a special utility class called the EntityManager. The EntityManager acts as a generic DAO (Data Access Object) for all entity beans in the JAR. It translates operations on entity beans to SQL statements to the
database. To obtain an EntityManager, the container creates one object and injects it into the session bean.

The addBook() and getAllBooks() methods in the BookCatalogBean class show the EntityManager in action. The EntityManager.persist() method takes a new
entity bean POJO and writes it to the database.

The code for the BookCatalogBean is given below.

package entity.library;
import java.util.Iterator;
import java.util.Collection;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import java.io.Serializable;
import javax.ejb.Remote;
@Remote(BookCatalogInterface.class)
@Stateless
public class BookCatalogBean implements
Serializable, BookCatalogInterface {
@PersistenceContext(unitName=”EntityBean”)
EntityManager em;
protected BookBank book;
protected Collection <BookBank> bookList;
public void addBook(String title, String
author, double price) {
// Initialize the form
if (book == null)
book = new BookBank(title, author, price);
em.persist(book);}
public Collection
<BookBank>getAllBooks() {
bookList=em.createQuery(“from BookBank
b”).getResultList(); return bookList;
}
}

 

Create BookBank entity bean class:

In the book catalog example, we define a BookBank entity bean class. The bean has three properties (title, author and price) to model a Book product. The id property is used to uniquely identify the Book bean instance by
the EJB3 container. The id value is automatically generated when the bean is saved to the database.

package entity.library;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.Collection;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name=”bookbank”)
public class BookBank implements Serializable
{
long id;
String title;
String author;
double price;
//protected Collection <LineItems>
lineitems;
public BookBank() {
super();
}
public BookBank(String title, String author,
double price) {
super();
this.title = title;
this.author = author;
this.price = price;
}
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
// Getter and setter methods for the

 
Feb 2008 | Java Jazz Up | 10
 
previous
index
next
 
View All Topics
All Pages of this Issue
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70 , 71 , 72 ,

Download PDF