Unstable Markup

Last modified by Administrator on 2011/06/06 17:43

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

  1. Open K -> Development -> MySQL Admin to view the existing databases and their users. (There are similar admin applications under Windows as well).
  2. Open console, type this command:
    mysql -u root -pStudent007
  3. Type these SQL commands:
    create database portaledu;
    use portaledu;
  4. Copy-paste the SQL script portaledu.sql? in the MySQL console window and see that it creates no errors
  5. 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
  6. 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

  1. Open JDeveloper 11
  2. Create a new application or pick an existing one
  3. Open with JDeveloper's menu View -> Database Navigator
  4. Right click "IDE Connections" in the "Databases" panel; pick New Connection.
    1. Connection name (write any name, e.g. "mysqlConnection"),
    2. Connection type - pick "Generic JDBC"
    3. Username: root
    4. Password: Student007
    5. 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.

      Configure the Persistance Unit

      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).
  5. Write the JDBC URL - "jdbc:mysql://localhost/portaledu".
  6. Check that you can browse the tables under the database "portaledu".
  7. Create an empty project under JDeveloper
  8. Right click the project name and pick "New" -? - Creating EJB from JDeveloper11.
  9. 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".
  10. See the book http://www.ante.lv/downloads/Manning.EJB.3.in.Action.Apr.2007.pdf.
  11. http://www.oracle.com/technology/products/ias/toplink/jpa/tutorials/jsf-jpa-tutorial.html - JPA and GlassFish tutorial (shows how to configure "persistence.xml").
Tags:
Created by Kalvis Apsītis on 2007/11/26 18:22

This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 3.0.36132 - Documentation