Exercise: Attach JPA to an Existing Table Structure
This exercise shows how to use EJB Entity objects (i.e. the JPA-annotated Java classes) to create a persistance layer from an existing database table structure.
Setting up the database
- Open K -> Development -> MySQL Admin to view the existing databases and their users. (There are similar admin applications under Windows as well).
- Open console, type this command:
mysql -u root -pStudent007
- Type these SQL commands:
create database portaledu;
use portaledu;
- Copy-paste the SQL script portaledu.sql? in the MySQL console window and see that it creates no errors
- Download the MySQL driver - http://www.mysql.com/products/connector/, pick link "Download" under "Connector/J"; unzip it:
cd /home/student/downloads/
gunzip mysql-connector-java-5.1.5.zip
- It should unpack a file mysql-connector-java-5.1.5-bin.jar among other things. This is the Java driver for MySQL database.
Configuring access to MySQL in JDeveloper11
- Open JDeveloper 11
- Create a new application or pick an existing one
- Open with JDeveloper's menu View -> Database Navigator
- Right click "IDE Connections" in the "Databases" panel; pick New Connection.
- Connection name (write any name, e.g. "mysqlConnection"),
- Connection type - pick "Generic JDBC"
- Username: root
- Password: Student007
- Driver class - press button [New], write driver class "com.mysql.jdbc.Driver"; press * "Entities from Tables".
1. Pick all the tables under the database and press ">>".
1. Create a "New" -> "EJB" -> "EJB Diagram".
1. Select all the Java classes (Content.java, ..., Variant.java) and drag them into the diagram.
1. It should create an UML model.
1. Open the "META-INF/persistence.xml" file and edit it: {code:xml}{code}
Bibliography
1. [http://www.oracle.com/technology/obe/obe11jdev/11/ejb/ejb.html" href="/xwiki/bin/edit/XWikiDevelopment/%22Business+Tier%22+%2D?parent=XWikiDevelopment.UnstableMarkup">Browse]*, select *[New]* and pick the MySQL driver (the JAR file unpacked in the previous subsection).
- Write the JDBC URL - "jdbc:mysql://localhost/portaledu".
- Check that you can browse the tables under the database "portaledu".
- Create an empty project under JDeveloper
- Right click the project name and pick "New" -? - Creating EJB from JDeveloper11.
- http://www.oracle.com/technology/tech/java/ejb30.html - See the link: "EJB 3 In Action : Code Examples for Oracle Application Sever 10g download zip".
- See the book http://www.ante.lv/downloads/Manning.EJB.3.in.Action.Apr.2007.pdf.
- http://www.oracle.com/technology/products/ias/toplink/jpa/tutorials/jsf-jpa-tutorial.html - JPA and GlassFish tutorial (shows how to configure "persistence.xml").