Question: create a NetBeans Java enterprise project with a JPA entity in the EJB module. When creating the enterprise project, you may omit the Web project
create a NetBeans Java enterprise project with a JPA entity in the EJB module. When creating the
enterprise project, you may omit the Web project module. The JPA entity should be created from a
PRODUCTS database table (see below). Use the NetBeans IDE derby services interface to create your
database and enter initial data before creating your enterprise project.
To test your JPA code, call it from a Java client (a Java class with a main method).
Add the Java client
to your EJB application module
. Your code must be able to select and display product data from the
PRODUCTS database table based on specific product ids (PID).
1.
Create a new database named productdb containing one table named PRODUCTS. This table
should be defined as follows.
PRODUCTS
PID
PNAME
PQTY
PPRICE
PID - 8 alphanumeric characters (e.g., P0001000).
Make this column the primary key
.
PNAME - 20 alphanumeric characters (e.g., circular saw)
PQTY - integer value representing quantity on hand
PPRICE - decimal value with two decimal places (e.g., 129.99)
2.
Insert initial test data into the PRODUCTS table of your database (at least 4 rows of data).
3.
Create a persistence.xml file by right-clicking on the EJB module and selecting New->
persistence->persistence-unit. This will create a persistence.xml file in the Configuration Files
folder of the EJB module. If you also see a glassfish-resources.xml file in the Configuration
Files/META-INF folder, delete it. Then, replace the contents of the persistence.xml file with the
statements given below.
(See page 2 below.)
4.
Create a JPA entity bean from the PRODUCTS table in the productdb database.
5.
Create a Java class (with a main method) that will call the JPA code to retrieve desired product
data, and then display the selected product data in an application output window. Add this Java
class to your EJB module source package. Copy and paste the main method shown below on
page 2 into this new Java class. Make sure you paste the main method inside the class code
block that was generated by NetBeans.
You will need to add appropriate code to the main
method in order to test the entity bean.
Chapter 4 of the Beginning Java EE 7 textbook, the
section titled Writing the Main Class provides examples of how to test entity bean code.
6.
Test your application with valid and invalid product id values. Your code should display a Not
Found message if the product id is invalid.
Submit one zip file (Assignment3.zip) containing
your entire NetBeans enterprise project
to the
appropriate Desire2Learn digital Dropbox.
However, you are not required to submit a copy of your
database files just a screenshot of the window showing the completed PRODUCTS table.
CIS 2858 Assignment 3 Java Persistence Entities (JPA)
public static void main(String[] args) {
Products product;
// Entity beans are managed by the Java EE Entity Manager. The following
// statements create an instance of the Entity Manager and begin a sequence
// within which queries may be executed against an entity bean.
EntityManagerFactory emf=Persistence.createEntityManagerFactory("Assignment3-ejbPU");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
// Insert your entity bean test statements here. Dont forget to
// add try-catch statements around any entity requests that may
// fail with no data found.
em.close();
emf.close();
}
persistence.xml file
xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"> source.Products
Potential name changes needed (highlighted in blue color)
?
The persistence unit name must match the one used in the createEntityManagerFactory
statement in the code on page 2 above.
?
The class reference
source.Products
must match the name of the Entity bean you created and
the package folder you saved it in.
?
The database name must match the database you created (...localhost:1527/
productdb
)
?
The database user name must match what you defined when the database was created.
?
The database password must match what you defined when the database was created.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
