After the BookDBAO object obtains an EntityManager instance, it can access data from the database. The getBooks method of BookDBAO calls the createQuery method of the EntityManager instance to retrieve a list of all books by bookId:
public List getBooks() throws BooksNotFoundException {
try {
return em.createQuery(
"SELECT bd FROM Book bd ORDER BY bd.bookId").
getResultList();
} catch(Exception ex){
throw new BooksNotFoundException("Could not get books: "
+ ex.getMessage());
}
}
The getBook method of BookDBAO uses the find method of the EntityManager instance to search the database for a particular book and return the associated Book instance:
public Book getBook(String bookId) throws BookNotFoundException {
Book requestedBook = em.find(Book.class, bookId);
if (requestedBook == null) {
throw new BookNotFoundException("Couldn’t find book: "
+ bookId);
}
return requestedBook;
}
The next section describes how Duke’s Bookstore performs updates to the data.