Previous Next vertical dots separating previous/next from contents/index/pdf

2. Generate O/R Mappings and Test with EJBQL

Time to complete this step: 15 minutes

The BEA Workshop ORM Workbench provides a powerful and flexible object relational mapping interface for popular persistence services like BEA Kodo JPA. Depending on the development scenario, OR Mappings can be generated through different mechanisms:

Generates annotated POJOs from schema using JPA ORM Generation wizard

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

  1. In the DbXplorer view, right-click the schema SalesDBConnection and select Generate JPA Mapping.

  2. Select JPA based Web application project, workshop-jpa-tutorial, from the Web Application dialog. Workshop considers a web application to be a JPA application if it has the Persistence file persistence.xml under the META-INF folder.

  3. Click OK.
  4. Select the database tables from the JPA ORM Generation dialog as shown below. Note that we are not selecting the CONTACT database table at present because we will create a POJO class Contact and annotate this class for JPA and that way we will understand the Top-Down development approach.

  5. You can also select the checkbox Copy driver and add to the class path in case its enabled. It instructs the JPA ORM Generation Wizard to add the JDBC driver for the Hypersonic Database to the project classpath. Currently the driver already exists in the project buildpath.
  6. Click Next
  7. The Table Associations dialog displays entity relationships as observed in the database schema from the foreign key definitions (shown in green). The dialog allows you to edit a table association by selecting each association and modifying its options in the editing panel.

  8. Select an association between LINEITEM and ORDER_DATA. Change the property from orderData to order.

  9. Select an association between LINEITEM and PRODUCT. We want to define many-to-one unidirectional relation between LINEITEM and PRODUCT. Hence, uncheck the checkbox for generating a reference to a set of LineItems in Product.

  10. Select an association between ORDER_DATA and CUSTOMER. Edit the property from orderDatas to orders.


  11. Customize the tables and columns mapping generation as described below:

    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, Workshop Studio 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.

    1. Review the following annotations in the class Customer.
      • Basic Properties - A basic property handles a standard value that is persisted as-is to the database.
      • A 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.
      @Basic()
      @Column(name="NAME", nullable=false, length=255)
      public String getName() {
      return this.name;
      }
      public void setName(String name) {
      this.name = name;
      }
      • 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.
      @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;
      }
      • 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).
      Id()
      @Column(name="CUSTOMERID", unique=true, nullable=false)
      public Integer getCustomerid() {
      return this.customerid;
      }
      public void setCustomerid(Integer customerid) {
      this.customerid = customerid;
      }
    2. Review the following annotations in the class OrderData.
      • 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 specifying 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;
      }

    Run simple EJBQL to ensure that the mappings were generated properly and the runtime is working

    In this step, we will take a tour of the EJBQL Editor. The EJBQL Editor lets you define and execute an EJBQL query.

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

 

Skip navigation bar   Back to Top