@Entity Table ConfigurationResin 3.0
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Quercus
Servlets and Filters
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Amber
Lifecycle
@Table
Tutorials
Lifecycle
Amber
Tutorials

Describes the basic annotation for a single-table entity bean.

  1. See Also
  2. Table annotations
    1. @Entity
    2. @SecondaryTable
    3. @Table
  3. Property Annotations
    1. @Basic
    2. @Column
  4. Primary Key Annotations
    1. @Id
    2. @GeneratedValue
  5. Relation annotations
    1. @JoinTable
    2. @JoinColumn
    3. @JoinColumns
    4. @ManyToMany
    5. @ManyToOne
    6. @OneToMany
    7. @OneToOne
  6. Inheritance annotations
    1. @DiscriminatorColumn
    2. @Inheritance
    3. InheritanceType

See Also

Table annotations

@Entity

Annotates the class as an entity bean.

See the basic property tutorial and the basic field tutorial for an introduction.

AttributeMeaningdefault
nameThe name of the beanThe class name (unqualified)

The fields or properties will be annotated by @Id, @Basic, etc. Amber will detect either field or property annotation by the type for the @Id. In other words, if Amber sees an @Id on a field, it will use field access. If Amber sees @Id on a method, it will use property access.

package javax.persistence;

@Target(TYPE)
@Retention(RUNTIME)
public @interface Entity {
  String name() default "";
}

@SecondaryTable

Specifies a secondary database table for an entity bean. The secondary table will contain the fields with a secondaryTable in the @Column.

AttributeMeaningdefault
nameThe name of the tableThe unqualified class name.
catalogthe table's catalognone
schemathe table's schemanone
pkJoinColumnsjoin column to the primary tablejoins the primary key
uniqueConstraintunique constraints during generationnone

package javax.persistence;

@Target(TYPE)
@Retention(RUNTIME)
public @interface SecondaryTable {
  String name() default "";
  String catalog() default "";
  String schema() default "";
  PrimaryKeyJoinColumn []pkJoinColumns() default {};
  UniqueConstraint []uniqueConstraints() default {};
}

@Table

Specifies the database table for an entity bean. The default table name is the class name.

AttributeMeaningdefault
nameThe name of the tableThe unqualified class name.
catalogthe table's catalognone
schemathe table's schemanone
uniqueConstraintunique constraints during generationnone

package javax.persistence;

@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {
  String name() default "";
  String catalog() default "";
  String schema() default "";
  UniqueConstraint []uniqueConstraints() default {};
}

Property Annotations

@Basic

Marks a field as a persistent field.

AttributeMeaningdefault
fetchEAGER or LAZY fetchingFetchType.EAGER
optionalif true, the column may be nulltrue

The fetch types are:

  • EAGER - fetch the field when the bean is loaded
  • LAZY - fetch the field only when the field is used

String property
@Entity
public class Course {
  @Basic
  public String getName()

  ...
}

Lazy-loaded property
@Entity
public class Course {
  @Basic(fetch=FetchType.LAZY)
  public String getMassiveText()

  ...
}
javax.persistence.Basic
package javax.persistence;

@Target({Method, FIELD})
@Retention(RUNTIME)
public @interface Basic {
  FetchType fetch() default EAGER;
  boolean optional() default true;
}

@Column

Specifies the field's SQL column name as well as any CREATE TABLE properties for auto generation.

AttributeMeaningdefault
nameThe SQL name of the columnthe field name
uniqueTrue for UNIQUE columnsfalse
nullableFalse for IS NOT NULL columnstrue
insertableTrue if column is inserted on a create calltrue
updatableTrue if column is updated when the field is modifiedfalse
columnDefinitionSQL to create the column in a CREATE TABLEnone
tablespecified if column is stored in a secondary tablenone
lengththe default length for a VARCHAR for a CREATE TABLE255
precisionthe default length for a number definition for a CREATE TABLE0
scalethe default length for a number definition for a CREATE TABLE0

String property
@Entity
public class Course {
  @Basic
  @Column(unique=true,
          nullable=false,
          length=32)
  public String getName()

  ...
}
javax.persistence.Column
@Target({Method, FIELD})
@Retention(RUNTIME)
public @interface Column {
  String name() default "";
  boolean unique() default false;
  boolean nullable() default true;
  boolean insertable() default true;
  boolean updateable() default true;
  String columnDefinition() default "";
  String table() default "";
  int length() default 255;
  int precision() default 0;
  int scale() default 0;
  boolean specified() default true;
}

Primary Key Annotations

@Id

Marks a field as a primary key. The @Id may be used in combination with @GeneratedValue to specify a generator for automatic key generation when new objects are created.

The default column name is "ID".

automatic generation
import javax.persistence.*;

@Entity
public class Course {
  @Id
  @Column(name="t_id")
  @GeneratedValue
  public long getId()

  ...
}
javax.persistence.Id
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Id {
}

@GeneratedValue

Used with @Id to specify a generator for automatic key generation when new objects are created.

AttributeMeaningdefault
strategyThe auto-generation typeAUTO
generatorThe sequence or table generator name${table}_cseq

The generator types are:

  • IDENTITY - the database supplies the new key, e.g. auto_increment, SERIAL, or IDENTITY
  • SEQUENCE - use a SEQUENCE type to generate the key
  • TABLE - use a @TableGenerator for the key
  • AUTO - choose the generator based on the database
    • MySQL - IDENTITY using auto_increment
    • Resin - IDENTITY using auto_increment
    • Postgres - SEQUENCE
    • Oracle - SEQUENCE

For SEQUENCE and TABLE, Resin will create the sequence name as "${table}_cseq".

automatic generation
import javax.persistence.*;

@Entity
public class Course {
  @Id
  @GeneratedValue
  public long getId()

  ...
}

sequence generation
import javax.persistence.*;

@Entity
public class Course {
  @Id
  @GeneratedValue(strategy=GeneratorType.AUTO
                  generator="COURSE_SEQ")
  public long getId()

  ...
}
javax.persistence.GeneratedValue
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface GeneratedValue {
  GenerationType strategy() default AUTO;
  String generator() default "";
}

Relation annotations

@JoinTable

Defines an association table for a many-to-many relation.

AttributeMeaningdefault
nameTable definition for the association tableconcatening the source and target table names
catalogDatabase catalog""
schemaDatabase schema""
joinColumnsColumns from from the association table to the source tableUses the source table primary key
inverseJoinColumnsColumns from from the association table to the target tableUses the target table primary key
javax.persistence.JoinTable
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface JoinTable {
  String table() default "";
  String catalog() default "";
  String schema() default "";
  JoinColumn []joinColumns() default {};
  JoinColumn []inverseJoinColumns() default {};
  UniqueContraint []uniqueConstraint() default {};
}

@JoinColumn

Defines a join (foreign) columns. Used for @ManyToOne.

See also @Column for corresponding definition for @Basic columns.

See the Many-to-One tutorial for a full example.

AttributeMeaningdefault
nameThe column name of the source tablethe column name of the target key
referencedColumnNameThe target column for composite keysthe single primary key
uniqueTrue if uniquefalse
nullableFalse if IS NOT NULLtrue
insertableTrue if the column is inserted on a createtrue
updateableTrue if the column is updated on field changestrue
columnDefinitionSQL column definitionfalse
tablespecifies a secondary table if not in the primarynone

Student to House link
public class Student {
  @Id
  @Column(name="student_id")
  long getId()

  @ManyToOne
  @JoinColumn(name="house_id")
  public House getHouse()
}

Student SQL
CREATE TABLE Student {
  student_id BIGINT PRIMARY KEY auto_increment

  house_id BIGINT REFERENCES House(id)
)
javax.persistence.JoinColumn
@Target({TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface JoinColumn {
  String name() default "";
  String referencedColumnName() default "";
  boolean unique() default false;
  boolean nullable() default false;
  boolean insertable() default true;
  boolean updateable() default true;
  String columnDefinition() default "";
  String table() default "";
}

@JoinColumns

Defines a set of join (foreign) columns for composite keys.

javax.persistence.ManyToOne
@Target({TYPE,METHOD, FIELD})
@Retention(RUNTIME)
public @interface JoinColumns {
  JoinColumn [] value() default{}
}

@ManyToMany

Marks a field as a many-to-many (association) relation.

The column names are the key columns of the source and target tables.

See the many-to-many tutorial for an example.

AttributeMeaningdefault
targetEntityThe class of the target entitythe property's type
cascadeOperations which cascade to the targetnone
fetchEAGER or LAZY fetchingFetchType.EAGER
mappedBySpecifies the source relation if a target

Simple link
@Entity
public class Student {
  @ManyToMany
  @JoinTable(
    name="student_course_map",
    joinColumns={@JoinColumn(name="student_id")},
    inverseJoinColumns={@JoinColumn(name="course_id")}
  )
  public Collection getCourses()

  ...
}
javax.persistence.ManyToMany
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface ManyToMany {
  String targetEntity default "";
  CascadeType []cascade() default {};
  FetchType fetch() default LAZY;
  String mappedBy isInverse() default "";
}

@ManyToOne

Marks a field as a many-to-one (link) relation.

The default column name is the column name of the target key.

See the many-to-one tutorial for an example.

AttributeMeaningdefault
targetEntityThe class of the target entitythe property's type
cascadeOperations which cascade to the targetnone
fetchEAGER or LAZY fetchingFetchType.EAGER
optionalIf false, the relation must always have a valuetrue

Simple link
@Entity
public class Student {
  @ManyToOne
  @JoinColumn(name="house")
  public House getHouse()

  ...
}
javax.persistence.ManyToOne
@Target({Method, FIELD})
@Retention(RUNTIME)
public @interface ManyToOne {
  String targetEntity default "";
  CascadeType []cascade() default {};
  FetchType fetch() default EAGER;
  boolean optional() default true;
}

@OneToMany

Marks a field as a one-to-many (collection) relation. Because a one-to-many field is dependent, it needs a @ManyToOne relation on the source table which defines the column.

AttributeMeaningdefault
targetEntityThe class of the target entitythe property's type
cascadeOperations which cascade to the targetnone
fetchEAGER or LAZY fetchingFetchType.EAGER
mappedBySpecifies the owning @ManyToOne property

Collection java
@Entity
public class House {
  ...
  @OneToMany(targetEntity=Student.class,
             mappedBy="house")
  public Collection getStudents()
}

@Entity
public class Student {
  ...
  @ManyToOne
  @JoinColumn(name="house")
  public House getHouse()
}

Collection SQL
CREATE TABLE House {
  id BIGINT PRIMARY KEY
)

CREATE TABLE Student {
  id BIGINT PRIMARY KEY,

  house BIGINT REFERENCES House(id)
)
javax.persistence.OneToMany
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface OneToMany {
  String targetEntity default "";
  CascadeType []cascade() default {};
  FetchType fetch() default EAGER;
  String mappedBy() default "";
}

@OneToOne

Marks a field as a one-to-one (dependent link) relation. Because a one-to-one field is dependent, it needs a @ManyToOne relation on the source table which defines the column.

AttributeMeaningdefault
targetEntityThe class of the target entitythe property's type
cascadeOperations which cascade to the targetnone
fetchEAGER or LAZY fetchingFetchType.EAGER
mappedBySpecifies the owning relation
javax.persistence.OneToOne
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface OneToOne {
  String targetEntity default "";
  CascadeType []cascade() default {};
  FetchType fetch() default EAGER;
  boolean optional() default true;
  String mappedBy() default "";
}

Inheritance annotations

@DiscriminatorColumn

Configures the discriminator column.

javax.persistence.DiscriminatorColumn
@Target(TYPE)
@Retention(RUNTIME)
public @interface DiscriminatorColumn {
  String name() default "";
  DiscriminatorType discriminatorType() default STRING;
  String columnDefinition() default "";
  int length() default 31;
}

@Inheritance

@Inheritance marks the entity bean as supporting inheritance, i.e. the database maps to different Java classes depending on a discriminator value.

javax.persistence.Inheritance
@Target(TYPE)
@Retention(RUNTIME)
public @interface Inheritance {
  InteritanceType strategy() default SINGLE_TABLE;
}

InheritanceType

javax.persistence.InheritanceType
public enum InheritanceType {
  SINGLE_TABLE,
  JOINED,
  TABLE_PER_CLASS
}

Lifecycle
Amber
Tutorials
Copyright © 1998-2006 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.