Entity Table ConfigurationResin 3.0
Resin 3.0

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

Bean Annotation
Table
Tutorials
Bean Annotation
EJB 3.0
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
  5. Relation annotations
    1. @AssociationTable
    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)
entityTypecontainer-managed (CMP) or bean-managed (BMP).CMP
accessFIELD or PROPERTYPROPERTY
versionThe EJB version3

An access type of PROPERTY uses bean-style setters and getters to implement CMP, i.e. each field Foo will have a getFoo and a setFoo method. The basic property tutorial describes this access type.

An access type of FIELD modifies the Java fields directly to impement CMP like JDO. The basic field tutorial describes this access type.

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

  EntityType entityType() default CMP;
  AccessType access() default PROPERTY;
  int version() default 3;
}

@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
joinjoin column to the primary tablejoins the primary key
uniqueConstraintunique constraints during generationnone

@Target(TYPE)
@Retention(RUNTIME)
public @interface SecondaryTable {
  String name() default "";
  String catalog() default "";
  String schema() default "";
  JoinColumn []join() 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

@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

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
@javax.ejb.Entity
public class Course {
  @javax.ejb.Basic
  public String getName()

  ...
}

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

  ...
}
javax.ejb.Basic
@Target({Method, FIELD})
@Retention(RUNTIME)
public @interface Basic {
  FetchType fetch() default EAGER;
}

@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
primaryKeyTrue for primary keyfalse
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
secondaryTablespecified 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
@javax.ejb.Entity
public class Course {
  @javax.ejb.Basic
  @javax.ejb.Column(unique=true,
                    nullable=false,
                    length=32)
  public String getName()

  ...
}
javax.ejb.Column
@Target({Method, FIELD})
@Retention(RUNTIME)
public @interface Column {
  String name() default "";
  boolean primaryKey() default false;
  boolean unique() default false;
  boolean nullable() default true;
  boolean insertable() default true;
  boolean updateable() default true;
  String columnDefinition() default "";
  String secondaryTable() 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 specify a generator for automatic key generation when new objects are created.

The default column name is "ID".

AttributeMeaningdefault
generateThe auto-generation typeNONE
generatorThe sequence or table generator name${table}_cseq

The generator types are:

  • NONE - no auto generation for this field
  • 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
@javax.ejb.Entity
public class Course {
  @javax.ejb.Id(generate=GeneratorType.AUTO)
  public long getId()

  ...
}

sequence generation
@javax.ejb.Entity
public class Course {
  @javax.ejb.Id(generate=GeneratorType.SEQUENCE,
                generator=COURSE_SEQ)
  public long getId()

  ...
}
javax.ejb.Id
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface Id {
  GeneratorType generate() default NONE;
  String generator() default "";
}

Relation annotations

@AssociationTable

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

AttributeMeaningdefault
tableTable definition for the association tableconcatening the source and target table names
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.ejb.AssociationTable
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface AssociationTable {
  Table table() default @Table(specified=false);
  JoinColumn []joinColumns() default {};
  JoinColumn []inverseJoinColumns() 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
primaryKeyTrue for a primary keyfalse
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
secondaryTablespecifies 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.ejb.JoinColumn
@Target({TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface JoinColumn {
  String name() default "";
  String referencedColumnName() default "";
  boolean primaryKey() default false;
  boolean unique() default false;
  boolean nullable() default false;
  boolean insertable() default true;
  boolean updateable() default true;
  String columnDefinition() default "";
  String secondaryTable() default "";
}

@JoinColumns

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

javax.ejb.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
isInverseIf true, this is the dependent sizefalse

Simple link
@javax.ejb.Entity
public class Student {
  @javax.ejb.ManyToMany
  @javax.ejb.AssociationTable(
    table=@javax.ejb.Table(name="student_course_map"),
    joinColumns=@JoinColumn(name="student_id"),
    inverseJoinColumns=@JoinColumn(name="course_id")
  )
  public Collection getCourses()

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

@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
@javax.ejb.Entity
public class Student {
  @javax.ejb.ManyToOne
  @javax.ejb.JoinColumn(name="house")
  public House getHouse()

  ...
}
javax.ejb.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

Collection java
@javax.ejb.Entity
public class House {
  ...
  @javax.ejb.OneToMany(targetEntity="Student")
  public Collection getStudents()
}

@javax.ejb.Entity
public class Student {
  ...
  @javax.ejb.ManyToOne
  @javax.ejb.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.ejb.OneToMany
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface OneToMany {
  String targetEntity default "";
  CascadeType []cascade() default {};
  FetchType fetch() default EAGER;
}

@OneToOne

Marks a field as a one-to-one (dependent link) relation. Because a one-to-one field is dependent, it needs a @OneToOne 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
javax.ejb.OneToOne
@Target({METHOD, FIELD})
@Retention(RUNTIME)
public @interface OneToOne {
  String targetEntity default "";
  CascadeType []cascade() default {};
  FetchType fetch() default EAGER;
  boolean optional() default true;
}

Inheritance annotations

@DiscriminatorColumn

Configures the discriminator column.

javax.ejb.DiscriminatorColumn
@Target(TYPE)
@Retention(RUNTIME)
public @interface DiscriminatorColumn {
  String name() default "";
  boolean nullable() default false;
  String columnDefinition() default "";
  int length() default 10;
}

@Inheritance

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

javax.ejb.Inheritance
@Target(TYPE)
@Retention(RUNTIME)
public @interface Inheritance {
  InteritanceType strategy() default SINGLE_TABLE;
  DiscriminatorType discriminatorType() default STRING;
  String discriminatorValue() default "";
}

InheritanceType

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

Bean Annotation
EJB 3.0
Tutorials
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.