Skip Headers

Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g Release 3 (10.1.3) B14428-01 |
| Home | Solution Area | Contents | Index

Previous | Next |

# Configuring Table and Column Information

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

- Configuring the Primary Table

- Configuring a Secondary Table

- Configuring a Column

- Configuring a Join Column

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.

Note: You can download an EJB 3.0 entity table and column code example from: `http://www.oracle.com/technology/tech/java/oc4j/ejb3/howtos-ejb3/howtoejb30mappingannotations/doc/how-to-ejb30-mapping-annotations.html`.

## Configuring the Primary Table

The primary table is the table into which the TopLink persistence manager persists your entity: in particular, it is the table that stores the entity's primary key (see "Configuring an EJB 3.0 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-5 shows how to use the `@Table` annotation to define the primary table for the `Employee` class. The TopLink persistence manager will persist instances of this entity to a table named `EJB_EMPLOYEE`.

Example 7-5 @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 Join Column") for persistent fields that are stored in that table. This allows you to distribute the persistent fields of an entity across multiple tables.

### Using Annotations

Example 7-6 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-6 @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 persistence 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 allows you to distribute the persistent fields of an entity across multiple tables.

### Using Annotations

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

Example 7-7 @Column for the Primary Table

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

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

Example 7-8 @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 a:

- secondary table (see Example 7-9)

- one-to-one mapping (see Example 7-10)

- many-to-one mapping (see Example 7-11)

- one-to-many mapping (see Example 7-12)

### Using Annotations

Example 7-9 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-9 @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-10 shows how to use the `@JoinColumn` annotation to specify a join column with a one-to-one mapping. For more information, see "Configuring a One-to-One Mapping".

Example 7-10 @JoinColumn with a One-to-One Mapping

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

Example 7-11 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-11 @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-12 shows how to use the `@JoinColumn` annotation to specify a join column with a one-to-many mapping. Fore more information, see "Configuring a One-to-Many Mapping".

Example 7-12 @JoinColumn with a One-to-Many Mapping

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