Oracle® Containers for J2EE Enterprise JavaBeans Developer's Guide 10g (10.1.3.1.0) Part Number B28221-02 |
|
|
View PDF |
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.
Note:
You can download a JPA 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
.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.
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
.
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.
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
.
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.
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
.
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:
a secondary table (see Example 7-13);
an one-to-one mapping (see Example 7-14);
a many-to-one mapping (see Example 7-15);
an one-to-many mapping (see Example 7-16);
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".