Make a Book
entity that contains the following fields. You may ignore the
id
: integer, primary key, automatically generatedtitle
: string, at most 255
characters long, must be presentauthor
: string, at most 255
characters long, must be presentpublishedAt
: date, optionalpages
: integer, optionalprice
: java.math.BigDecimal
with precision "10,2"
, optionalStockStatus
: enum values stored as strings
IN_STOCK
OUT_OF_STOCK
UNKNOWN
Store the books in the database table books
.
equals()
and hashCode()
so that they function based on the primary key.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.
Create an entity Author
that represents the author of a book:
id
: integer, primary key, automatically generatedname
: string, at most 255
characters long, compulsorybirthDate
: date - only year/month/day, optionalCopy the Book
entity (as described in the Database entity section) to your new project.
author
field, an Author
entity. Call the inverse side of this relation books
.
equals()
and hashCode()
so that they function based on the primary key.
@ToString(of={"..."})
annotation.Implement the following functionalities.
// Adds an author.
Author addAuthor(String name, Date birthDate /* can be null */);
// Removes an author.
void removeAuthor(Author author);
// Adds a book with one author. It will be UNKNOWN in stock.
Book addBook(String title, Author author);
// Associates a book with an author.
void assignAuthorToBook(Author author, Book book);
// Removes a book.
void removeBook(Book book);
books
.Experiment with different CascadeType
settings for the connections between the books and the authors.
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.
GET
requests.
a
with the value b
.0
) in the session scope.
Try the application in various ways, and see what happens.
Create a web application called business
.
GET
requests.
/companies
, let the servlet return a page with a list of (built-in) company names./products
, let the servlet return a page with a list of (built-in) product names.request.getServletPath()
to determine which path is being accessed.company.jsp
.
Map<String, List<String>>
field in your servlet to store the company names and their associated products./companies
. Let the JSP file list all companies.POST
method.POST
requests, too.product.jsp
.
/company/COMPANY_NAME
.
COMPANY_NAME
is not a valid company name, set the status code to SC_NOT_FOUND
(404
).web.xml
to redirect to an error.html
file upon page not found.input
of type hidden
to distinguish between this form and the previous one.Company
and Product
.Map
.
jdbc/company
in GlassFish.The setup file contains a reminder on how to create EJBs.
Company
with the interface CompanyInterface
.
hasProduct
that takes a String
and returns a boolean
value.
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.To create a RESTful service, you will need a Dynamic Web Module. Have a look at web.xml
for configuration options.
@WebService
with a @WebMethod
named isBetween
that takes three parameters. Let it return true
iff param1
is between param2
and param3
.
@WebMethod
s: setLowerBound
, setUpperBound
and isBetween
. The implementing class will store the bounds, and isBetween
only takes one number; it will check whether the parameter value is between the two stored values.Map
, and associate them with a List
of their product names.GET
request to /companies
will return the names of all companies in a comma separated string.GET
request to /company/FamousCompany
will return the product names for FamousCompany
in a comma separated string.POST
request to /add/NewCompany
and /add/NewCompany/NewProduct
. (Here, NewCompany
and NewProduct
are arbitrary names.)/company/FamousCompany/xml
and /company/FamousCompany/json
. Create the class Company
that stores its products in a list of strings, and use JAXB annotations to produce the XML and JSON.File>New>JPA Entity
) called TestEntity
in the package test.entity
, which is a simple class annotated with @Entity
.
persistence.xml
(under JPA Content
in Project Explorer
) in the Managed classes
part. Also, have a look at the Source
tab.int
field called id
. It will be the entity’s primary key: annotate it with @Id
. Also, add @GeneratedValue
.MyEntity
instances: create some of them, persist them and query them.persistence.xml
settings, e.g. try to set eclipselink.ddl-generation
to drop-and-create-tables
instead of create-tables
.Company
, Person
and Product
classes from before to entities.
id
for each. Annotate them with @Id
.