Difference between revisions of "EclipseLink/UserGuide/JPA/Advanced JPA Development/Schema Generation/Index"

From Eclipsepedia

Jump to: navigation, search
(30 intermediate revisions by 2 users not shown)
Line 6: Line 6:
 
|api=y
 
|api=y
 
|apis=  
 
|apis=  
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Index.html Index]
+
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Index.html @Index]
 +
|nativeapi=y
 +
|nativeapis=
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/tools/schemaframework/IndexDefinition.html IndexDefinition]
 
* [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/tools/schemaframework/IndexDefinition.html IndexDefinition]
 
}}
 
}}
  
 
+
=@Index=
=@Index Annotation and <index> XML=
+
 
+
This section is in progress...
+
  
 
An index is a database structure defined for a table, to improve query and look-up performance for a set of columns.  
 
An index is a database structure defined for a table, to improve query and look-up performance for a set of columns.  
Use the @Index annotation in code or the <index> element in the orm.xml desciptor to create an index on a table and to have EclipseLink's DDL generator generate it.
+
Use the [http://www.eclipse.org/eclipselink/api/latest/org/eclipse/persistence/annotations/Index.html <tt>@Index</tt>] annotation in code or the <tt><index></tt> element in the <tt>eclipselink-orm.xml</tt> descriptor to create an index on a table.  
  
An index can be defined on an Entity class or on an attribute:
+
An index can be defined on an entity or on an attribute. For the entity it must define a set of columns to index.
* For the Entity it must define a set of columns to index. The table is defaulted.
+
* For an attribute, the table and column are defaulted.
+
The name of the index defaults to _<column>_INDEX, but a name should be provided.
+
  
An IndexDefinition in the schema framework allows defining, creating, and dropping indexes using code.  
+
Index creation is database specific. Some databases may not support indexes. Most databases auto-index primary key and foriegn key columns. Some databases support advanced index DDL options. To create more advanced index DDL, a DDL script or native query can be used.
  
The @Index annotation creates an IndexDefinition and stores it on the descriptor's DatabaseTable.  
+
{{EclipseLink_AttributeTable
 +
|caption=@Index Attributes
 +
|content=
 +
<tr>
 +
<td>'''<tt>catalog</tt>'''</td>
 +
<td>The catalog of the INDEX.</td>
 +
<td>Default catalog </td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>columnNames</tt>'''</td>
 +
<td>Specify the set of columns to define the index on. </td>
 +
<td>For an Entity , the table. For an attribute, the table and column.</td>
 +
<td>Not required when annotated on a field or method.</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>name</tt>'''</td>
 +
<td>The name of the INDEX.</td>
 +
<td><tt>&lt;table&gt;_<column>_INDEX</tt> (but a name should be provided)</td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>schema</tt>'''</td>
 +
<td>The schema of the INDEX.</td>
 +
<td>Default schema</td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>table</tt>'''</td>
 +
<td>The table to define the index on, defaults to entities primary table.</td>
 +
<td>The entity's primary table.</td>
 +
<td>No</td>
 +
</tr>
 +
<tr>
 +
<td>'''<tt>unique</tt>'''</td>
 +
<td>Specify whether the index is unique or non-unique.</td>
 +
<td><tt>false</tt></td>
 +
<td>No</td>
 +
</tr>
 +
}}
  
During default schema generation the DefaultSchemaGenerator adds the IndexDefinition to the TableDefinition to be created.  
+
This example defines three indexes, one on first name, one on last name, and a multiple column index on first name and last name.
 +
<source lang="java">
 +
@Entity
 +
@Index(name="EMP_NAME_INDEX", columns={"F_NAME","L_NAME"})
 +
public class Employee{
 +
    @Id
 +
    private long id;
 +
    @Index
 +
    @Column(name="F_NAME")
 +
    private String firstName;
 +
    @Index
 +
    @Column(name="L_NAME")
 +
    private String lastName;
 +
    ...
 +
}
 +
</source>
  
The IndexDefinition is used to create indexes for primary key and unique constraints. Support for indexes is defined in the platform. By default a platform is assumed to support indexes. MySQL requires special drop syntax to include the table name in the drop statement.
 
  
 +
You can also create an index in the <tt>eclipselink-orm.xml</tt> descriptor using <tt><index></tt>, as shown in the following example. Define columns using the <tt><column></tt> subelement. All the attributes supported in the <tt>@Index</tt> annotation are also supported in the <tt><index></tt> element.
  
= Configuration File =
 
* orm.xml
 
 
<source lang="xml">
 
<source lang="xml">
 
<index name="EMP_NAME_INDEX" table="EMPLOYEE" unique="true">
 
<index name="EMP_NAME_INDEX" table="EMPLOYEE" unique="true">
Line 42: Line 90:
  
 
{{EclipseLink_JPA
 
{{EclipseLink_JPA
 +
 
|previous =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation/CascadeOnDelete|@CascadeOnDelete]]
 
|previous =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation/CascadeOnDelete|@CascadeOnDelete]]
 
|up      =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]]
 
|up      =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation|Schema Generation]]
|next =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Performance|Performance]]
+
|next =[[EclipseLink/UserGuide/JPA/Advanced_JPA_Development/Schema_Generation/Appending_strings_to_CREATE_TABLE_statements|Appending Strings to CREATE TABLE Statements]]
 
|version=2.2.0 DRAFT}}
 
|version=2.2.0 DRAFT}}

Revision as of 10:07, 21 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

Elug api package icon.png Native API


@Index

An index is a database structure defined for a table, to improve query and look-up performance for a set of columns. Use the @Index annotation in code or the <index> element in the eclipselink-orm.xml descriptor to create an index on a table.

An index can be defined on an entity or on an attribute. For the entity it must define a set of columns to index.

Index creation is database specific. Some databases may not support indexes. Most databases auto-index primary key and foriegn key columns. Some databases support advanced index DDL options. To create more advanced index DDL, a DDL script or native query can be used.

@Index Attributes
Attribute Description Default Required?
catalog The catalog of the INDEX. Default catalog No
columnNames Specify the set of columns to define the index on. For an Entity , the table. For an attribute, the table and column. Not required when annotated on a field or method.
name The name of the INDEX. <table>_<column>_INDEX (but a name should be provided) No
schema The schema of the INDEX. Default schema No
table The table to define the index on, defaults to entities primary table. The entity's primary table. No
unique Specify whether the index is unique or non-unique. false No

This example defines three indexes, one on first name, one on last name, and a multiple column index on first name and last name.

 
@Entity
@Index(name="EMP_NAME_INDEX", columns={"F_NAME","L_NAME"})
public class Employee{
    @Id
    private long id;
    @Index
    @Column(name="F_NAME")
    private String firstName;
    @Index
    @Column(name="L_NAME")
    private String lastName;
    ...
}


You can also create an index in the eclipselink-orm.xml descriptor using <index>, as shown in the following example. Define columns using the <column> subelement. All the attributes supported in the @Index annotation are also supported in the <index> element.

<index name="EMP_NAME_INDEX" table="EMPLOYEE" unique="true">
    <column>F_NAME</column>
    <column>L_NAME</column>
</index>

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