Step 2. Generate O/R Mappings and Test with EJBQL

Time to complete this step: 15 minutes

Oracle Enteprise Pack for Eclipse (OEPE) provides a powerful and flexible object relational mapping interface for popular persistence services like OpenJPA. Depending on the development scenario, O/R mappings can be generated through different mechanisms.

  • Reverse Engineering the Schema to create object relational mappings
  • Generating Mappings from an Object Model
  • The tasks you will complete in this step are:

     

    To Generate Annotated POJOs from a Schema using the JPA ORM Generation Wizard

    In this step, we will use OEPE to automatically generate JPA entity beans from an existing database schema by reverse engineering the schema. OEPE will generate Java classes with the appropriate accessors and JPA annotations.

    1. Open the JPA perspective if it's not already open. Window > Open Perspective > other > JPA
    2. In the Project Explorer, right-click the project oepe-jpa-tutorial and select JPA > Generate Entities from Tables...

    3. Select the database tables from the Generate Custom Entities dialog as shown below.

      Do not select the CONTACT database table. Later on in this tutorial you will use the CONTACT database table to manually create a POJO class Contact and annotate this class for JPA in order to better understand the top-down development approach.

    4. Click Next.
    5. The Table Associations dialog displays entity relationships as observed in the database schema from the foreign key definitions. The dialog allows you to edit a table association by selecting each association and modifying its options in the editing panel.

      Select the association between LINEITEM and ORDER_DATA.
      Change the value of the first Property field from orderData to order.

    6. Select the association between LINEITEM and PRODUCT.
      We want to define many-to-one unidirectional relationship between LINEITEM and PRODUCT. Uncheck the checkbox Generating a reference to a set of LineItems in Product.

    7. Select the association between ORDER_DATA and CUSTOMER.
      Change the value of the second Property field from orderData to orders.
    8. Click Next.
    9. In the Customize Default Entity Generation dialog, confirm that none is selected in the Key Generator field.

      Set Entity Access to Property, and for Collection Properties Type, verify that java.util.List is selected.

      A Java Package is required for generating the entity beans. Enter oracle.beans as the Domain Java Class (as shown below) and the package will be created automatically upon completion of the dialog.

      Click Next.
    10. The Customize Individual Entities dialog allows you to customize the tables and columns mapping generation. We will not customize the tables in this tutorial, but note that you can how tables and columns can generate flexible, maintainable java source.

    11. Click Finish.
    12. You can access the entities through the Project Explorer view under the Java Resources: Java branch of the project. In the Project Explorer view, you can find the object model under the package com.bea.beans of the branch web/WEB-INF/src/java.
      1. To view the set of Persistence entities generated by OEPE, navigate to the persistence.xml file within your web project. OEPE identifies the persistent entity- based class names of entities specified in persistence.xml file.

    To Review Generated Classes with Annotations

    When we create entity mappings, we define each property as having one of six property types: basic, id, many-to-one, one-to-one, one-to-many, and many-to-many. When you generate entities from a database, OEPE annotates the generated source code with JPA annotations that designate which type a given property is. In this section we will review some basic JPA annotations.

    Review the following annotations in the class Customer.

    1. Double-click the file oepe-jpa-tutorial > web/WEB-INF/src/java > com.bea.beans > Customer.java to view its source.
    2. Basic Properties - A basic property handles a standard value that is persisted as-is to the database.

      The @Basic annotation is followed by the @Column annotation defining an attribute name which is the name of the column to which the property is bound; the attribute nullable is false to specify that the column cannot store null values; the column length attribute specifies the maximum length.
    3. public String getName() {

      return this.name;

      }

      public void setName(String name) {

      this.name = name;

      }

    4. One-to-Many Properties - A one-to-many property designates a relationship in which one A entity references multiple B entities, and no two A entities reference the same B entity.

      Here a Customer entity references multiple OrderData entities, but an OrderData entity can have only one Customer entity reference.

      The one-to-many annotation defines an attribute mappedBy which is the name of the many-to-one field in OrderData entity that maps this bidirectional relation.
    5. @OneToMany(mappedBy="customer", fetch=FetchType.EAGER)
      public java.util.Set<OrderData> getOrders() {
      return this.orders;
      }
      public void setOrders(java.util.Set<OrderData> orders) {
      this.orders = orders;
      }
    6. Id properties - An Id property designates an identifier, such as a primary key. All entity beans must declare one or more fields which together form the persistent identity of an instance.

      An Id annotation is followed by a @Column annotation defining the attribute unique which is true to specify that the column is UNIQUE in the SQL sense (can have only unique values).
    7. Id()

      public Integer getCustomerid() {

      return this.customerid;

      }

      public void setCustomerid(Integer customerid) {

      this.customerid = customerid;

      }

    8. Double-click the class file OrderData.java.

      Many-to-one Properties - A many-to-one property designates a relationship in which an entity A references a single entity B, and other A's might also reference the same B; there is a many-to-one relation from A to B.

      The many-to-one annotation defines an attribute fetch which is a enum that specifies whether to load the field's persisted data before the entity object is returned by the persistence provider (FetchType.EAGER) or later, when the property is accessed (FetchType.LAZY).

      The many- to-one annotation is followed by an @JoinColumn annotation defining the column name which is the name of the column to which the property is bound and a referencedColumnName attribute which is the name of the primary key column being joined to.
      @ManyToOne(fetch=FetchType.EAGER)
      @JoinColumn(name="CUSTOMERID", referencedColumnName="CUSTOMERID")
      public Customer getCustomer() {
      return this.customer;
      }
      public void setCustomer(Customer customer) {
      this.customer = customer;
      }

    Click one of the following arrows to navigate through the tutorial: