Databases: JPA

Database entity

Make a Book entity that contains the following fields. You may ignore the

Store the books in the database table books.

JPA

Make a class BookStore that handles the above Book entities via JPA. Implement the following operations:

// Constructor, gets the name of a persistence unit, and creates an appropriate `EntityManager` for it.
BookStore(String persistenceUnitName);

// Adds a new book into the database, and returns its identifier.
int addBook(Book newBook);

// Deletes the book with the given id (if it exists in the database).
void removeBookById(int bookId);

// Gets all books present in the database.
java.util.List<Book> getAllBooks();

Make a command line application that tests the BookStore class.

JPA relations

Create an entity Author that represents the author of a book:

Copy the Book entity (as described in the Database entity section) to your new project.

Enterprise Applications

Servlets

Make a project with a web application ScopeTest that contains a servlet. From the context root, make the servlet accessible under /test.

The servlet has to do the following.

Try the application in various ways, and see what happens.

Servlets: JSP, JPA

Create a web application called business.

  1. Make a single servlet that responds to GET requests.
  2. Make a JSP page company.jsp.
  3. Make a JSP page product.jsp.
  4. Change the servlet to use JPA.

Stateless EJB

The setup file contains a reminder on how to create EJBs.

  1. Make an application that has a stateless EJB named Company with the interface CompanyInterface.
    1. In the EJB and its interface, make a method hasProduct that takes a String and returns a boolean value.
      • The product names that the company produces can be built into the company as a “magic constant” (a set of strings) for now.
      • Make a JNDI client, too, and test the application with it.
    2. Now, support product names in several languages the following way.
      • Make local beans for each language: CompanyProductsEn, CompanyProductsHu etc. contain the list of product names in the appropriate language. The local beans have a method getProductNames that simply return the list.
      • Company will have a separate variable for each language, and the appropriate local EJB will be injected into it.
      • hasProduct now takes an additional parameter, a String for the language. The method returns false if the language is not known, otherwise it takes the products from the local bean, and returns whether the product name is present in the list.

RESTful services: JAX-RS, JAXB

To create a RESTful service, you will need a Dynamic Web Module. Have a look at web.xml for configuration options.

  1. Make a @WebService with a @WebMethod named isBetween that takes three parameters. Let it return true iff param1 is between param2 and param3.
  2. Make such a RESTful service that can be used to store company names and the names of their products.

Miscellaneous exercises

JPA

  1. Make an entity (File>New>JPA Entity) called TestEntity in the package test.entity, which is a simple class annotated with @Entity.
    1. Note that the class is listed in persistence.xml (under JPA Content in Project Explorer) in the Managed classes part. Also, have a look at the Source tab.
    2. Let it have an int field called id. It will be the entity’s primary key: annotate it with @Id. Also, add @GeneratedValue.
  2. Experiment with MyEntity instances: create some of them, persist them and query them.
  3. Experiment with persistence.xml settings, e.g. try to set eclipselink.ddl-generation to drop-and-create-tables instead of create-tables.
  4. Convert the Company, Person and Product classes from before to entities.
    1. First, only convert the fields with built-in types.
    2. You will have to add an id for each. Annotate them with @Id.