Recommended: Sing it, brah! 5 fabulous songs for developers
JW's Top 5
Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs
Page 5 of 5
One product can be included in multiple orders. Once you have mapped the owning side, the inverse side becomes very easy to map, as you can see in Listing 12.
@Entity(name = "PRODUCT")
public class Product {
@Id
@Column(name = "PROD_ID", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private long prodId;
@Column(name = "PROD_NAME", nullable = false,length = 50)
private String prodName;
@Column(name = "PROD_DESC", length = 200)
private String prodDescription;
@Column(name = "REGULAR_PRICE", precision = 2)
private String price;
@Column(name = "LAST_UPDATED_TIME")
private Date updatedTime;
@ManyToMany(mappedBy="productList",fetch=FetchType.EAGER)
private List<Order> orderList;
...............
The other attributes and getters and setters goes here
}
Listing 13 illustrates how to fetch all the orders for a particular product.
..........
EntityManagerFactory entityManagerFactory =
Persistence.createEntityManagerFactory("testjpa");
EntityManager em = entityManagerFactory.createEntityManager();
Product product = em.find(Product.class, 2000);
System.out.println("Order details for product : " + product.getOrderList());
em.close();
entityManagerFactory.close();
..........
Now, cast your mind back to the example application that started this tutorial. The sample company, XYZ, wants to find out the following:
Now that the relationships have been defined, this can be very easily achieved with just a few lines of code, as shown in Listing 14.
..............
Query query = em.createQuery("SELECT customer FROM CUSTOMER customer");
List list= query.getResultList();
for(Customer customer:list){
List prodList = new ArrayList();
List prodListCancelled = new ArrayList();
if(customer.getOrders()!=null){
for(Order allOrders: customer.getOrders()){
if(allOrders.getInvoice().getOrderCancelledDt() == null){
//Find out how many products each customer has
prodList.addAll(allOrders.getProductList());
}else{
//Find out how many products cancelled by each customer
prodListCancelled.addAll(allOrders.getProductList());
}
}
}
}
..............
The code in Listing 14 retrieves all customers from the CUSTOMER table. For each customer, it retrieves the number of products
ordered, filters the uncanceled orders, and adds them to prodList. It also retrieves the number of products for each cancelled order for each customer, and puts that number into prodListCancelled.
Thus, prodList contains products used by a particular customer, and prodListCancelled contains products cancelled by a particular customer. And with that information in hand, XYZ can easily solicit the customers
it's most interested in for its customer satisfaction surveys.
This article's sample application was designed to demonstrate a variety of types of data relationships. In this article, you have seen how complex CRUD operations involving multiple relational tables can be handled in an object- oriented fashion using the Java Persistence API. This can drastically reduce the complexity of enterprise application queries and maintenance overhead.
JPA has certainly simplified data persistence by providing a standard API usable in both Java SE and EE environments. Hopefully the sample application shown here will give you ideas on how JPA can help you in your own projects.
Aditi Das is a technical architect with Infosys Technologies and has seven years of specialized experience in Java and JEE. She is a Sun-certified enterprise architect (SCEA), Web component developer (SCWCD), business component developer (SCBCD), and Web service developer (SCDJWS). She is very much inspired by the Head First philosophy of learning new technologies, and hopes that someday a book will come out on the past, present, and future of SOA.
Archived Discussions (Read only)