Skip to main content

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.

Jump to: navigation, search

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

(Example: Using quoted @Table)
(Example: Using quoted @Table)
Line 103: Line 103:
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
 
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Table|@Table]]
 
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Table|@Table]]
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Mapping|Mapping]]
+
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Mapping|Mapping]]
 
|up=      [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Creating_and_Configuring_Entities|Configuring Entities]]
 
|up=      [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Creating_and_Configuring_Entities|Configuring Entities]]
 
|version=2.2.0 DRAFT}}
 
|version=2.2.0 DRAFT}}

Revision as of 11:41, 16 June 2011

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Elug api package icon.png Key API

@SecondaryTable

You can use the @Table annotation or <table> XML element to configure the table for an entity to do the following:

  • 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
Elug javaspec icon.gif

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

@Table Attributes
Attribute Description Default Required?
name The name of the table entity name (as uppercase) No
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", uniqueConstraints = {@UniqueConstraint(columnNames={"SSN"})})
public class Employee implements Serializable {
    ...
    @Id
    public Long getId() {
        return id;
    }
    ...
}
Example: Using <table-generator>
<entity class="Employee">
    <table name="EMP">
        <unique-constraint>
            <column-name>SSN</column-name>
        </unique-constraint>
    </table>
    <attributes>
        <id name="id"/>
        ...
    </attributes>
</entity>


Views

It is possible to map an entity to a database view. To do this simply give the view name for the name in the @Table 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 " 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
@Entity
@Table(name="\"Employee Data\"")
public class Employee implements Serializable {
    ...
    @Id
    public Long getId() {
        return id;
    }
    ...
}

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

Copyright © Eclipse Foundation, Inc. All Rights Reserved.