package com.sapient.impl;
import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import com.sapient.entities.Book;
import com.sapient.spec.BookDAO;
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class BookDAOEJB implements BookDAO{
@PersistenceContext
private EntityManager em;
@Resource
private UserTransaction userTransaction;
@Override
public void saveBook(Book b) {
System.out.println("---- saveBook of BookDAOEJB called ----");
try {
userTransaction.begin();
System.out.println("---- Begin User Trasanction ----");
em.persist(b);
userTransaction.commit();
System.out.println("---- Commit User Trasanction ----");
} catch (Exception e) {
e.printStackTrace();
try {
userTransaction.rollback();
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
@Override
public Book findBookById(String id) {
Book bookFromDB = em.find(Book.class, id);
System.out.println("---- BookDAO EJB Loading book with id "+id);
return bookFromDB;
}
}
Wednesday, 21 December 2011
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment