Java Reference
In-Depth Information
@Entity
@EntityListeners(LogEntityListener.class)
public class Student implements Serializable {
//
}
New annotations
JPA 2.1 added an annotation (
@Index
) to create indexes on tables when a schema
is generated from entities and an annotation (
@ForeignKey
) to designate foreign
keys of a table.
The
@Index
annotation has one mandatory parameter (
columnList
) to list the
columns that make up the index with different sort orders. It also has two optional
parameters: the
name
parameter, which allows you to change the default name of
the index, and the
unique
parameter to set the index as unique or not unique.
In parallel,
@Index
annotation was added as a part of
Table
,
SecondaryTable
,
CollectionTable
,
JoinTable
, and
TableGenerator
annotations.
The
@ForeignKey
can be used as element of
JoinColumn
,
JoinColumns
,
MapKeyJoinColumn
,
MapKeyJoinColumns
,
PrimaryKeyJoinColumn
,
PrimaryKeyJoinColumns
,
CollectionTable
,
JoinTable
,
SecondaryT-
able
, and
AssociationOverride
annotations to either define or modify the for-
eign key constraints on a table. It takes three parameters: name, value for the con-
straint, and the definition of the foreign key. The three parameters are optional.
An example of an entity with a foreign key and indexed columns is shown in the fol-
lowing code:
@Entity
@Table(indexes = @Index(columnList = "name ASC,
id DESC"))
public class MyEntity implements Serializable {
@Id
private Long id;
private String name;