Sunday, May 25, 2008

The Power of JPA

JPA has provided a useful method to persist entities in a quick and repeatable fashion. Using JPA as an Object Relational Mapping tool (ORM), a developer can rapidly create the persistence layer to the point where the db is another service layer that is managed through annotations within the code.

Here's an example of another entity class...

package reviewmodule.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

* @author Rob
@Table(name = "product_category")
@NamedQueries({@NamedQuery(name = "ProductCategory.findByProdId", query = "SELECT p FROM ProductCategory p WHERE p.product = :prod"), @NamedQuery(name = "ProductCategory.findByCategory", query = "SELECT p FROM ProductCategory p WHERE p.category = :category")})
public class ProductCategory implements Serializable {

private static final long serialVersionUID = 1L;
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "PRODCAT_ID")
private long id;

@JoinColumn(name = "PROD_ID", nullable=false)
private Product product;

@JoinColumn(name = "CATEGORY_ID", nullable=false)
private Category category ;

public ProductCategory() {

public ProductCategory(Product product, Category category){

public long getId() {
return id;

public void setId(long id) { = id;

public Product getProduct() {
return product;

public void setProduct(Product product) {
this.product = product;

public Category getCategory() {
return category;

public void setCategory(Category category) {
this.category = category;


The entity itself contains the information on primary keys, preset queries, foreign keys, db table relations, class information, and much more. This makes the job of returning and managing an object from the db simple.

For example, all that is needed to find all of the Product classes assigned to a specific Category class would be the following...

Query query2 = em.createNamedQuery("ProductCategory.findByCategory") ;
query2.setParameter("category", category);
productList = query2.getResultList();

Where category is an instance of the Category class and the Named Query "ProductCategory.findByCategory is defined in the ProductCategory Entity class above as follows...

query = "SELECT p FROM ProductCategory p WHERE p.category = :category"

The getResultList method will return a List of Product Objects. All that remains to persist the ProductCategory join table is to add this class to the persistence.xml and to get an instance of the entity manager. The following code will use a factory method to create an instance of the entity manager.

EntityManagerFactory emf;

EntityManager em;

String PERSISTENCE_UNIT_NAME = "ReviewModulePU";

emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);

em = emf.createEntityManager();

Category category = null ;

try {

Query query = em.createNamedQuery("Category.findByDescription");

query.setParameter("description", getSessionBean1().getSelectedCategory().getDescription());

category = (Category) query.getSingleResult();

Query query2 = em.createNamedQuery("ProductCategory.findByCategory") ;

query2.setParameter("category", category);

productList = query2.getResultList();

setProdByCatTable(new ReviewListTableBean[productList.size()]);

for (int i=0;i<productList.size();i++) {

if (productList.get(i).getProduct() != null) {

Product specificProduct = productList.get(i).getProduct() ;

ReviewListTableBean PICBean = new ReviewListTableBean() ;



PICBean.setImageUrl(specificProduct.getImageUrl()) ;


getProdByCatTable()[i]=PICBean ;



} catch (Exception qe) {

System.out.println(this.getClass().toString() + " Exception: " + qe.toString());

} finally {




No comments: