HTML and CSS Reference
In-Depth Information
if (book.getTitle() != null) {
updatableBook.setTitle(book.getTitle());
}
}
}
@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
@Stateless
public class BookManager implements BookManagerLocal {
@PersistenceContext(unitName = "megaAppUnit")
EntityManager em;
@Override
public Book getBookInformation(Integer bookID) throws BookNotFound {
Query query = em.createQuery("select book.id, book.isbn, book.title, "
+ "book.author, book.publisher, book.lang from Book book where "
+ "book.id = :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],
null);
return book;
}
 
Search WWH ::




Custom Search