Jump to: navigation, search

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

Line 11: Line 11:
  
 
=@SecondaryTable=
 
=@SecondaryTable=
You can use the <tt>@Table</tt> annotation or <code><nowiki><table></nowiki></code> XML element to configure the table for an entity to do the following:
+
You can use the <tt>@SecondaryTable</tt> annotation or <code><nowiki><secondary-table></nowiki></code> 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.
* define the name of the entity's table
+
* define the schema or catalog if your table is defined in a different schema than your connection and requires to be prefixed
+
* define unique constraints for DDL generation
+
  
{{EclipseLink_Spec|section=Section 11.1.xx "Table Annotation"}}
+
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 <code>OneToOne</code> and <code>OneToMany</code>.
 +
 
 +
{{EclipseLink_Spec|section=Section 11.1.xx "SecondaryTableAnnotation"}}
 
{{EclipseLink_AttributeTable
 
{{EclipseLink_AttributeTable
|caption=@Table Attributes
+
|caption=@SecondaryTable Attributes
 
|content=<tr>
 
|content=<tr>
 
  <td>'''<tt>name</tt>'''</td>
 
  <td>'''<tt>name</tt>'''</td>
 
  <td>The name of the table
 
  <td>The name of the table
  <td>entity name (as uppercase)</td>
+
  <td></td>
  <td>No</td>
+
  <td>Yes</td>
 
</tr>
 
</tr>
 
<tr>
 
<tr>
Line 53: Line 52:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@Table(name="EMP", uniqueConstraints = {@UniqueConstraint(columnNames={"SSN"})})
+
@Table(name="EMP")
 +
@SecondaryTable(name="SALARY")
 
public class Employee implements Serializable {
 
public class Employee implements Serializable {
 
     ...
 
     ...
Line 68: Line 68:
 
<source lang="xml">
 
<source lang="xml">
 
<entity class="Employee">
 
<entity class="Employee">
     <table name="EMP">
+
     <table name="EMP"/>
        <unique-constraint>
+
    <secondary-table name="SALARY"/>
            <column-name>SSN</column-name>
+
        </unique-constraint>
+
    </table>
+
 
     <attributes>
 
     <attributes>
 
         <id name="id"/>
 
         <id name="id"/>
Line 81: Line 78:
 
<br>
 
<br>
  
==Views==
+
==Advanced Multiple Table Configuration==
It is possible to map an entity to a database view.  To do this simply give the view name for the name in the <code>@Table</code> annotation.
+
  
==Case Sensitively, Reserved Words and Special Characters==
 
Some databases are case sensitive, but most are not.  In general it is best to uppercase all table/column names to be portable.
 
Most databases allow mixed cased names if you put the name in quotes.  To do this use the <code>"</code> character when defining the table's name.  Quotes also allow the usage of spaces, reserved words, and some special characters on most databases.
 
 
======''Example: Using quoted @Table''======
 
<source lang="java">
 
@Entity
 
@Table(name="\"Employee Data\"")
 
public class Employee implements Serializable {
 
    ...
 
    @Id
 
    public Long getId() {
 
        return id;
 
    }
 
    ...
 
}
 
</source>
 
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA

Revision as of 13:42, 16 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.

Elug javaspec icon.gif

For more information, see Section 11.1.xx "SecondaryTableAnnotation" in the JPA Specification.

@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


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...