Skip Headers
Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide
10g (10.1.3.5.0)

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

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

Configuring Table and Column Information

You can define the characteristics of the database table into which the TopLink entity manager persists your entity, including the following:

This is particularly important if you have an existing database schema.

If you do not have an existing database schema, you can delegate table and column definition to OC4J by omitting this configuration: at deployment time, OC4J will create default table and column names based on class and data member names.

Configuring the Primary Table

The primary table is the table into which the TopLink entity manager persists your entity: in particular, it is the table that stores the entity's primary key (see "Configuring a JPA Entity Primary Key"). Optionally, you can also specify one or more secondary tables (see "Configuring a Secondary Table"), if the entity's persistent data is stored across multiple tables.

You define the primary table at the entity class level.

Using Annotations

Example 7-9 shows how to use the @Table annotation to define the primary table for the Employee class. The TopLink entity manager will persist instances of this entity to a table named EJB_EMPLOYEE.

Example 7-9 @Table

@Entity
@Table(name="EJB_EMPLOYEE")
public class Employee implements Serializable {
...
}

Configuring a Secondary Table

Specifying one or more secondary tables indicates that the entity's persistent data is stored across multiple tables. You must first specify a primary table (see "Configuring the Primary Table") before you can specify any secondary tables.

You define a secondary table at the entity class level.

If you specify one or more secondary tables, you can specify the secondary table name in the column definition (see "Configuring a Column") for persistent fields that are stored in that table. This enables the distribution of entity persistent fields across multiple tables.

Using Annotations

Example 7-10 shows how to use the @SecondaryTable annotation to specify that some of the entity's persistent data is stored in a table named EJB_SALARY.

Example 7-10 @SecondaryTable

@Entity
@Table(name="EJB_EMPLOYEE")
@SecondaryTable(name="EJB_SALARY")
public class Employee implements Serializable {
...
}

Configuring a Column

The column is, by default, the name of the column in the primary table (see "Configuring the Primary Table"), into which the TopLink entity manager stores the field's value.

You define the column at one of the property (getter or setter method) or field level of your entity.

If you specified one or more secondary tables (see "Configuring a Secondary Table"), you can specify the secondary table name in the column definition. This enables the distribution of entity persistent fields across multiple tables.

Using Annotations

Example 7-11 shows how to use the @Column annotation to specify column F_NAME in the primary table for field firstName.

Example 7-11 @Column for the Primary Table

@Column(name="F_NAME")
public String getFirstName() {
    return firstName;
}

Example 7-12 shows how to use the @Column annotation to specify column SALARY in secondary table EMP_SALARY for field salary.

Example 7-12 @Column for a Secondary Table

@Column(name="SALARY", secondaryTable="EMP_SALARY")
public String getSalary() {
    return salary;
}

Configuring a Join Column

A join column specifies a mapped, foreign key column for joining an entity association or a secondary table.

You can define a join column with the following:

Using Annotations

Example 7-13 shows how to use the @JoinColumn annotation to specify a join column with a secondary table. For more information, see "Configuring a Secondary Table".

Example 7-13 @JoinColumn With a Secondary Table

@Entity
@Table(name="EJB_EMPLOYEE")
@SecondaryTable(name="EJB_SALARY")
@JoinColumn(name="EMP_ID", referencedColumnName="EMP_ID")
public class Employee implements Serializable {
...
}

Example 7-14 shows how to use the @JoinColumn annotation to specify a join column with an one-to-one mapping. For more information, see "Configuring an One-to-One Mapping".

Example 7-14 @JoinColumn With an One-to-One Mapping

@OneToOne(cascade=ALL, fetch=LAZY)
@JoinColumn(name="ADDR_ID")
public Address getAddress() {
    return address;
}

Example 7-15 shows how to use the @JoinColumn annotation to specify a join column with a many-to-one mappiong. For more information, see "Configuring a Many-to-One Mapping".

Example 7-15 @JoinColumn With a Many-to-One Mapping

@ManyToOne(cascade=PERSIST, fetch=LAZY)
@JoinColumn(name="MANAGER_ID", referencedColumnName="EMP_ID")
public Employee getManager() {
    return manager;
}

Example 7-16 shows how to use the @JoinColumn annotation to specify a join column with an one-to-many mapping. Fore more information, see "Configuring an One-to-Many Mapping".

Example 7-16 @JoinColumn With an One-to-Many Mapping

@OneToMany(cascade=PERSIST)
@JoinColumn(name="MANAGER_ID", referencedColumnName="EMP_ID")
public Collection getManagedEmployees() {
    return managedEmployees;
}