Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/SecondaryTable"

Line 12: Line 12:
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/ClassDescriptor.html ClassDescriptor]
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/ClassDescriptor.html ClassDescriptor]
 
** [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/ClassDescriptor.html#addTableName(java.lang.String) addTable()]
 
** [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/ClassDescriptor.html#addTableName(java.lang.String) addTable()]
** [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/ClassDescriptor.html#addForeignKeyFieldNameForMultipleTable(java.lang.String,java.lang.String)
+
** [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/ClassDescriptor.html#addForeignKeyFieldNameForMultipleTable(java.lang.String,java.lang.String)
 
  addForeignKeyFieldNameForMultipleTable()]
 
  addForeignKeyFieldNameForMultipleTable()]
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/DescriptorQueryManager.html DescriptorQueryManager]
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/descriptors/DescriptorQueryManager.html DescriptorQueryManager]

Revision as of 10:47, 20 June 2011

EclipseLink JPA

@SecondaryTable

You can use the @SecondaryTable annotation or <secondary-table> XML element to configure an entity to map to multiple tables. When an entity instance is persisted, it will insert into each of the tables. When an entity instance is read, the tables will be joined. This allows for the entity to define mappings that make use of any of the columns in any of the tables.

Secondary tables should only be used when the entity logically has its data spread across multiple tables. If the tables represent relationships, then this is better modeled using relationship mappings such as OneToOne and OneToMany.

By default the secondary table is joined to the primary table's id columns. This assumes that the secondary table has the same id columns defined with the same name. If the secondary table uses different names for the id columns, then a set or @PrimaryKeyJoinColumns must be provided. @PrimaryKeyJoinColumn can also be used for JOINED inheritance to specify how the child table is joined to the parent table. @PrimaryKeyJoinColumn still requires that the secondary table have all of the id columns of the primary table, for more advanced multiple table joins see Advanced Multiple Table Configuration.

@SecondaryTable Attributes
Attribute Description Default Required?
name The name of the table Yes
catalog A String catalog name. Default catalog for database No
schema The String name of the schema. Default schema of the database No
uniqueConstraints This is used only by DDL generation. By default only a primary key and foreign key constraints are defined, if desired set the value of this attribute to an array of one or more UniqueConstraint instances.
Elug javaspec icon.gif

For more information, see Section 11.1.49 "UniqueConstraint Annotation" in the JPA Specification.

No additional constraints No
Elug javaspec icon.gif

For more information, see Section 11.1.42 "SecondaryTable Annotation" in the JPA Specification.

@PrimaryKeyJoinColumn Attributes
Attribute Description Default Required?
name The name foreign key column in the secondary table that references the id column in the primary table id column No
referencedColumnName A name of the id column in the primary table that is being referenced. This is only required for composite ids. id column No
columnDefinition Optional column description for use with DDL generation. No
Elug javaspec icon.gif

For more information, see Section 11.1.40 "PrimaryKeyJoinColumn Annotation" in the JPA Specification.

The following example shows how to use this annotation to specify the table for Employee.

Example: Using @Table
@Entity
@Table(name="EMP")
@SecondaryTable(name="SALARY")
public class Employee implements Serializable {
    ...
    @Id
    public Long getId() {
        return id;
    }
    ...
}
Example: Using <table-generator>
<entity class="Employee">
    <table name="EMP"/>
    <secondary-table name="SALARY"/>
    <attributes>
        <id name="id"/>
        ...
    </attributes>
</entity>


Advanced Multiple Table Configuration

Eclipselink-logo.gif
Version: 2.2.0 DRAFT
Other versions...