Describes the basic annotation for a single-table entity bean.
@Entity
Annotates the class as an entity bean.
See the basic property tutorial
and the basic field tutorial
for an introduction.
Attribute | Meaning | default |
name | The name of the bean | The class name (unqualified)
|
entityType | container-managed (CMP) or bean-managed (BMP). | CMP
|
access | FIELD or PROPERTY | PROPERTY
|
version | The EJB version | 3
|
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.
Attribute | Meaning | default |
name | The name of the table | The unqualified class name.
|
catalog | the table's catalog | none
|
schema | the table's schema | none
|
join | join column to the primary table | joins the primary key
|
uniqueConstraint | unique constraints during generation | none
|
@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.
Attribute | Meaning | default |
name | The name of the table | The unqualified class name.
|
catalog | the table's catalog | none
|
schema | the table's schema | none
|
uniqueConstraint | unique constraints during generation | none
|
@Target(TYPE)
@Retention(RUNTIME)
public @interface Table {
String name() default "";
String catalog() default "";
String schema() default "";
UniqueConstraint []uniqueConstraints() default {};
}
|
@Basic
Marks a field as a persistent field.
Attribute | Meaning | default |
fetch | EAGER or LAZY fetching | FetchType.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.
Attribute | Meaning | default |
name | The SQL name of the column | the field name
|
primaryKey | True for primary key | false
|
unique | True for UNIQUE columns | false
|
nullable | False for IS NOT NULL columns | true
|
insertable | True if column is inserted on a create call | true
|
updatable | True if column is updated when the field is modified | false
|
columnDefinition | SQL to create the column in a CREATE TABLE | none
|
secondaryTable | specified if column is stored in a secondary table | none
|
length | the default length for a VARCHAR for a CREATE TABLE | 255
|
precision | the default length for a number definition for a CREATE TABLE | 0
|
scale | the default length for a number definition for a CREATE TABLE | 0
|
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;
}
|
@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".
Attribute | Meaning | default |
generate | The auto-generation type | NONE
|
generator | The 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 "";
}
|
@AssociationTable
Defines an association table for a many-to-many relation.
Attribute | Meaning | default |
table | Table definition for the association table | concatening the source and target table names
|
joinColumns | Columns from from the association table to the source table | Uses the source table primary key
|
inverseJoinColumns | Columns from from the association table to the target table | Uses 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.
Attribute | Meaning | default |
name | The column name of the source table | the column name of the target key
|
referencedColumnName | The target column for composite keys | the single primary key
|
primaryKey | True for a primary key | false
|
unique | True if unique | false
|
nullable | False if IS NOT NULL | true
|
insertable | True if the column is inserted on a create | true
|
updateable | True if the column is updated on field changes | true
|
columnDefinition | SQL column definition | false
|
secondaryTable | specifies a secondary table if not in the primary | none
|
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.
Attribute | Meaning | default |
targetEntity | The class of the target entity | the property's type
|
cascade | Operations which cascade to the target | none
|
fetch | EAGER or LAZY fetching | FetchType.EAGER
|
isInverse | If true, this is the dependent size | false
|
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.
Attribute | Meaning | default |
targetEntity | The class of the target entity | the property's type
|
cascade | Operations which cascade to the target | none
|
fetch | EAGER or LAZY fetching | FetchType.EAGER
|
optional | If false, the relation must always have a value | true
|
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.
Attribute | Meaning | default |
targetEntity | The class of the target entity | the property's type
|
cascade | Operations which cascade to the target | none
|
fetch | EAGER or LAZY fetching | FetchType.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.
Attribute | Meaning | default |
targetEntity | The class of the target entity | the property's type
|
cascade | Operations which cascade to the target | none
|
fetch | EAGER or LAZY fetching | FetchType.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;
}
|
@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
}
|
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark,
and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc. | |
|