Skip Headers
Oracle® SOA Suite Developer's Guide
10g (10.1.3.1.0)

Part Number B28764-01
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Index
Index
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
View PDF

4.2 Creating JPA Entities that Map to Database Tables

The easiest way to create JPA entities that map to database tables is to use the Create Entities from Tables wizard. The wizard guides you through the process of obtaining or creating a database connection, choosing the tables you want to use, and creating JPA entities.

The Create Entities from Tables wizard is available in the New Gallery, in the Business Tier section.


Tip:

When you create entities from tables, you can create more than one at the same time. If the tables in the database are related by foreign key constraints, fields will be created and annotated in the generated entities. For this reason, it's often a best practice to generate all your entities at the same time.

4.2.1 What Happens When You Create JPA Entities From Database Tables

When you create JPA entities from tables, new Java classes are created to support each entity. If there are relationships between the database tables, these are discovered and preserved via annotations in the source code.

In Example 4-3, notice the annotations in the source code that begin with the @ symbol. Annotations vastly simplify working with EJB entities.

Example 4-1 Customer Entity Created From a Database Table

package org.soademo.customerservice.persistence;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
import javax.persistence.TableGenerator;
 
@Entity
@NamedQueries( { @NamedQuery(name = "Customer.findAllCustomer", 
                             query = "select object(o) from Customer o")
        , 
        @NamedQuery(name = "Customer.findCustomerById", query = "select object(cust) from Customer cust where cust.custid = :custid")
        , 
        @NamedQuery(name = "Customer.findCustomerByEmail", query = "select object(cust) from Customer cust where cust.email = :email and cust.password = :password")
        } )
 
@Table(name = "CUSTOMER")
@SequenceGenerator(name = "SEQ_ID_GENERATOR", sequenceName = "EJB_SEQ_ID_GEN")
@TableGenerator(name = "TABLE_ID_GENERATOR", table = "EJB_TAB_ID_GEN", 
                pkColumnName = "ID_NAME", valueColumnName = "SEQ_VALUE", 
                pkColumnValue = "SEQ_GEN")
public class Customer implements Serializable {
    private String creditcardnumber;
    private String creditcardtype;
    @Id
    @GeneratedValue(strategy = GenerationType.TABLE, 
                    generator = "TABLE_ID_GENERATOR")
    @Column(nullable = false)
    private String custid;
    private String email;
    private String fname;
    private String lname;
    private String phonenumber;
    private String status;
    private String password;
    
    @OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL } )
    @JoinTable(name = "CUSTOMER_ADDRESS", 
               joinColumns = { @JoinColumn(name = "CUSTID")
                } , inverseJoinColumns = { @JoinColumn(name = "ADDRESSID")
                } )
    private List<Address> addressList;
 
    public Customer() {
    }
 
    public String getCreditcardnumber() {
        return creditcardnumber;
    }
 
    public void setCreditcardnumber(String creditcardnumber) {
        this.creditcardnumber = creditcardnumber;
    }
 
    public String getCreditcardtype() {
        return creditcardtype;
    }
 
    public void setCreditcardtype(String creditcardtype) {
        this.creditcardtype = creditcardtype;
    }
 
 
    public String getCustid() {
        return custid;
    }
 
    public void setCustid(String custid) {
        this.custid = custid;
    }
 
    public String getEmail() {
        return email;
    }
 
    public void setEmail(String email) {
        this.email = email;
    }
 
    public String getFname() {
        return fname;
    }
 
    public void setFname(String fname) {
        this.fname = fname;
    }
 
    public String getLname() {
        return lname;
    }
 
    public void setLname(String lname) {
        this.lname = lname;
    }
 
    public String getPhonenumber() {
        return phonenumber;
    }
 
    public void setPhonenumber(String phonenumber) {
        this.phonenumber = phonenumber;
    }
 
    public String getStatus() {
        return status;
    }
 
    public void setStatus(String status) {
        this.status = status;
    }
 
    public List<Address> getAddressList() {
        if (addressList == null) {
            addressList = new ArrayList();
        }
        return addressList;
    }
 
    public void setAddressList(List<Address> addressList) {
        this.addressList = addressList;
    }
 
    public Address addAddress(Address address) {
        getAddressList().add(address);
        return address;
    }
 
    public Address removeAddress(Address address) {
        getAddressList().remove(address);
        return address;
    }
 
    public void setPassword(String password) {
        this.password = password;
    }
 
    public String getPassword() {
        return password;
    }
}

4.2.2 What You May Need to Know About Creating Entities From Tables

After creating an EJB entity from a database table, you can modify the generated annotations by hand. This is an advanced topic and not covered in this chapter.