@ManyToOne
From CauchoWiki
The @ManyToOne relation represents a unidirectional link to another entity bean. In the database, it is represented by a foreign key column.
The property type must be a configured entity bean.
@ManyToOne
public ParentBean getParent() { return _parent; }
Contents |
[edit] Annotation Definition
public @interface ManyToOne {
Class targetEntity() default void.class;
CascadeType []cascade() default {};
FetchType []fetch() default EAGER;
boolean optional() default true;
}
The targetEntity gives the class name of the target entity bean. If unspecified, it defaults to the return type of the property.
The CascadeType selects operations that are cascaded to the target when the source bean is modified.
The EAGER FetchType is a hint to Amber that the field should be fetched along with the rest of the entity. The LAZY fetch type is a hint that Amber can wait until the field is used before loading it from the database.
The optional value indicates that the field is Nullable in the database. Java primitives are never optional.
[edit] See Also
The following annotations can be used in combination with @ManyToOne.
- @JoinColumn specifies a database foreign key column name and definition
- @JoinColumns specifies a set of foreign key columns used with a compound primary key
- Relation catalog
[edit] Column names
If a @JoinColumn is specified with a @ManyToOne property, the @JoinColumn's name specifies the column name for the foreign key. If there is no @JoinColumn, the column name is constructed from the property name and the target key name.
@ManyToOne public TargetBean getNext()
public class TargetBean {
@Id
String getName() { return _name; }
}
The foreign key name would be "NEXT_NAME".
[edit] Example: @ManyToOne default mapping
@Entity public class Src { @Id int getId() { ... } @ManyToOne Dst getParent() { ... } } @Entity public class Dst { @Id int getId() { ... } }
create table SRC (
ID integer primary key
PARENT_ID integer references(DST, ID)
)
create table DST (
ID integer primary key
)
[edit] Example: @ManyToOne specified SQL
@Entity @Table(name="s") public class Src { @Id @Column(name="s_id") int getId() { ... } @ManyToOne @JoinColumn(name="parent") Dst getParent() { ... } } @Entity @Table(name="d") public class Dst { @Id @Column(name="d_id") int getId() { ... } }
create table s (
s_id integer primary key,
parent integer references(d, d_id)
)
create table d (
d_id integer primary key
)
