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

From Eclipsepedia

Jump to: navigation, search
(New page: {{EclipseLink_UserGuide |info=y |toc=n |eclipselink=y |eclipselinktype=JPA |api=y |apis= * [http://www.eclipse.org/eclipselink/api/latest/javax/persistence/Table.html @Table] }} =@Table= ...)
 
 
(7 intermediate revisions by one user not shown)
Line 1: Line 1:
 
{{EclipseLink_UserGuide
 
{{EclipseLink_UserGuide
 
|info=y
 
|info=y
|toc=n
+
|toc=y
 
|eclipselink=y
 
|eclipselink=y
 
|eclipselinktype=JPA
 
|eclipselinktype=JPA
Line 11: Line 11:
 
=@Table=
 
=@Table=
 
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>@Table</tt> annotation or <code><nowiki><table></nowiki></code> XML element to configure the table for an entity to do the following:
* define the name of the entity's table, because the name is awkward, a reserved word, incompatible with a preexisting data model, or invalid as a table name in your database;
+
* 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 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
 
* define unique constraints for DDL generation
  
{{EclipseLink_Spec|section=Section 11.1.xx "Table Annotation"}}
 
 
{{EclipseLink_AttributeTable
 
{{EclipseLink_AttributeTable
 
|caption=@Table Attributes
 
|caption=@Table Attributes
Line 45: Line 44:
 
}}
 
}}
  
 +
{{EclipseLink_Spec|section=Section 11.1.45 "Table Annotation"}}
  
 
The following example shows how to use this annotation to specify the table for <code>Employee</code>.
 
The following example shows how to use this annotation to specify the table for <code>Employee</code>.
Line 52: Line 52:
 
<source lang="java">
 
<source lang="java">
 
@Entity
 
@Entity
@Table(name="EMP")
+
@Table(name="EMP", uniqueConstraints = {@UniqueConstraint(columnNames={"SSN"})})
 
public class Employee implements Serializable {
 
public class Employee implements Serializable {
 
     ...
 
     ...
Line 67: Line 67:
 
<source lang="xml">
 
<source lang="xml">
 
<entity class="Employee">
 
<entity class="Employee">
 +
    <table name="EMP">
 +
        <unique-constraint>
 +
            <column-name>SSN</column-name>
 +
        </unique-constraint>
 +
    </table>
 
     <attributes>
 
     <attributes>
         <id name="id">
+
         <id name="id"/>
            <column name="EMP_ID"/>
+
            <generated-value generator="Emp_Gen" strategy="TABLE"/>
+
            <table-generator name="Emp_Gen" allocationSize="1"/>
+
        </id>
+
 
         ...
 
         ...
 
     </attributes>
 
     </attributes>
Line 79: Line 80:
 
<br>
 
<br>
  
====Views====
+
==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 <code>@Table</code> annotation.
 
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====
+
==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.
 
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.
 
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.
Line 102: Line 103:
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
 
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/SequenceGenerator|@SequenceGenerator]]
 
|previous=[[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/SequenceGenerator|@SequenceGenerator]]
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/Ids/SecondaryTable|@SecondaryTable]]
+
|next=    [[EclipseLink/UserGuide/JPA/Basic JPA Development/Entities/SecondaryTable|@SecondaryTable]]
 
|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}}

Latest revision as of 13:15, 20 June 2011

EclipseLink JPA

link="http://wiki.eclipse.org/EclipseLink"
EclipseLink
Website
Download
Community
Mailing ListForumsIRC
Bugzilla
Open
Help Wanted
Bug Day
Contribute
Browse Source

Elug api package icon.png Key API

Contents


[edit] @Table

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

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

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

[edit] Example: Using @Table
@Entity
@Table(name="EMP", uniqueConstraints = {@UniqueConstraint(columnNames={"SSN"})})
public class Employee implements Serializable {
    ...
    @Id
    public Long getId() {
        return id;
    }
    ...
}
[edit] 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>


[edit] 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.

[edit] 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.

[edit] 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...