Thursday, 31 May 2012

Understanding JPA entities

  • A JPA entity is a java class referring to one row of a table
  • Each entity has same number fields/properties as in table structure
  • It can be with or without relationship
  • that is One to one, One to many or many to many
  • Consider the following table structure for one to many relationship
  • For above table structure two entity will be created
  • Categories.java (manage Categories table)
  • Products.java (manage product table)



//////////////////////////////////////////////////////////////////////
// CATEGORIES

package entities;

import java.io.Serializable;
import javax.persistence.*;
import java.util.List;

/**
* The persistent class for the Categories database table.
*
*/
@Entity
@Table(name="Categories")
public class Categories implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="CategoryId", unique=true, nullable=false)
private String categoryId;

@Column(name="CategoryName")
private String categoryName;

@Column(name="Description", length=50)
private String description;

//bi-directional many-to-one association to Products
@OneToMany(mappedBy="category", cascade={CascadeType.PERSIST, CascadeType.REMOVE})
private List products;

 public Categories() {
 }

public String getCategoryId() {
return this.categoryId;
}

public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}

public Object getCategoryName() {
return this.categoryName;
}

public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}

public String getDescription() {
return this.description;
}

public void setDescription(String description) {
this.description = description;
}

public List getProducts() {
return this.products;
}

public void setProducts(List products) {
this.products = products;
}

}
//////////////////////////////////////////////////////////////////////
// PRODUCTS
package entities;


import java.io.Serializable;
import javax.persistence.*;


/**
* The persistent class for the Products database table.
*
*/
@Entity
@Table(name="Products")
public class Products implements Serializable {
private static final long serialVersionUID = 1L;

@Id
@Column(name="ProductId", unique=true, nullable=false)
private String productId;

@Column(name="ProductName", length=50)
private String productName;

@Column(name="Supplier", length=50)
private String supplier;

//bi-directional many-to-one association to Categories
 @ManyToOne
@JoinColumn(name="CategoryId")
private Categories category;

 public Products() {
 }

public String getProductId() {
return this.productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getProductName() {
return this.productName;
}

public void setProductName(String productName) {
this.productName = productName;
}

public String getSupplier() {
return this.supplier;
}

public void setSupplier(String supplier) {
this.supplier = supplier;
}

public Categories getCategory() {
return this.category;
}

public void setCategory(Categories category) {
this.category = category;
}

}
  • Above two class represent two tables Categories and products
  • You need to import javax.persistence.*
  • Using above classes & EntityManager we can fetch data from database
  • Consider the following code ....

public static void main()
{
 // Creating entity manager to create Db query
 EntityManagerFactory factory = Persistence.createEntityManagerFactory("WebWithJPA");
 javax.persistence.EntityManager em = factory.createEntityManager();
 
 try 
 {
  // Creating Query using JPQL
  Query objQuery = em.createQuery("select c from Categories c");
 
  // Getting result set in List
  List objC = (List) objQuery.getResultList();
  
  // Listing all categories and its related products
  for (Categories c : objC) 
  {
   System.out.println("Category Name: " + c.getCategoryName());
   for (Products p : c.getProducts()) 
   {
    System.out.println("* " + p.getProductName());
   }
  }
}