HTML and CSS Reference
In-Depth Information
if (book.getTitle() != null) {
@Stateless annotation defines BookManager class as a stateless session EJB. @PersistenceContext annotation is
used for injecting a container-managed entity manager instance, using which we will be able to perform the database
operations. The registerBook method checks if an already existing book ISBN matches the new book ISBN, and
if it finds a case, it throws BookAlreadyExists exception, or else it persists the new book data in the database. The
updateBook method throws BookNotFound exception if the topic to be updated does not exist in the database; if the
target book exists, it merges the new data updates with the existing book data using the mergeBookAttrs method.
Listing 13-5 shows the remaining methods of BookManager EJB.
Listing 13-5. BookManager EJB's Remaining Methods
public class BookManager implements BookManagerLocal {
@PersistenceContext(unitName = "megaAppUnit")
EntityManager em;
public Book getBookInformation(Integer bookID) throws BookNotFound {
Query query = em.createQuery("select, book.isbn, book.title, "
+ ", book.publisher, book.lang from Book book where "
+ " = :id");
query.setParameter("id", bookID);
Object[] bookInfo = null;
try {
bookInfo = (Object[]) query.getSingleResult();
} catch (NoResultException exception) {
throw new BookNotFound(exception.getMessage());
Book book = new Book(
(Integer) bookInfo[0],
(String) bookInfo[1],
(String) bookInfo[2],
(String) bookInfo[3],
(String) bookInfo[4],
(String) bookInfo[5],
return book;
Search WWH ::

Custom Search