Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/SecondaryTable
EclipseLink JPA
EclipseLink | |
Website | |
Download | |
Community | |
Mailing List • Forums • IRC • mattermost | |
Issues | |
Open • Help Wanted • Bug Day | |
Contribute | |
Browse Source |
Key API
Contents
@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
.
For more information, see Section 11.1.xx "SecondaryTableAnnotation" in the JPA Specification.
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.
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>