@OneToMany

From CauchoWiki

Jump to: navigation, search


The @OneToMany relation represents a unidirectional link to a collection of entity beans. In the database, it is represented by a join table with foreign keys to the source entity and to the target entity.

The property type must be a collection.

 @OneToMany
 public Collection<ChildBean> getChildren() { return _children; }

Contents

[edit] Annotation Definition

 public @interface OneToMany {
     Class targetEntity() default void.class;
     CascadeType []cascade() default {};
     FetchType []fetch() default EAGER;
     String mappedBy() default "";
 }

The targetEntity gives the class name of the target entity bean. If unspecified, it defaults to the item type of the collection.

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 @OneToMany.

  • @JoinTable specifies the mapping table and foreign keys.
  • @ManyToOne serves as the source field for bidirectional mappings

[edit] Tutorials

[edit] Column names

If a @JoinTable is specified with a @OneToMany property, the @JoinTable's name specifies the name of the join table. If there is no @JoinTable, the join table name is constructed from the source table and the target table name.

    @Entity
    public class SourceBean {
         @Id
         public int getId() { ... }

         @OneToMany
         public TargetBean getNext()
   }

   @Entity
   public class TargetBean {
         @Id 
         String getName() { return _name; }
   }

The join table name would be SOURCEBEAN_TARGETBEAN, and the default foreign keys would be "SOURCEBEAN_ID" and "TARGETBEAN_ID".

[edit] Examples

[edit] Example: unidirectional @OneToMany default mapping

The unidirectional @OneToMany mapping is a collection where the link information is stored in an intermediate @JoinTable.

   @Entity
   public class Src {
       @Id 
       int getId() { ... }
 
       @OneToMany
       Collection<Dst> getChildren() { ... }
   }

   @Entity
   public class Dst {
       @Id
       int getId() { ... }
    }
   create table SRC (
       ID integer primary key
   )
   
   create table DST (
       ID integer primary key
   )

   create table SRC_DST (
       SRC_ID integer references(SRC, ID),

       DST_ID integer references(DST, ID)
   )

[edit] Example: unidirectional @OneToMany with specified SQL

   @Entity
   @Table(name="s")
   public class Src {
       @Id 
       @Column(name="s_id")
       int getId() { ... }
 
       @OneToMany
       @JoinTable(name="map",
                  joinColumns={@JoinColumn(name="s_key")},
                  inverseJoinColumns={@JoinColumn(name="d_key")})
       Collection<Dst> getChildren() { ... }
   }

   @Entity
   @Table(name="d")
   public class Dst {
       @Id
       @Column(name="d_id")
       int getId() { ... }
    }
   create table s (
       s_id integer primary key
   )
   
   create table d (
       d_id integer primary key
   )

   create table map (
       s_key integer references(s, s_id),

       d_key integer references(d, d_id)
   )

[edit] Example: bidirectional @OneToMany default mapping

The bidirectional @OneToMany mapping is a collection where the link information by a corresponding @ManyToOne.

   @Entity
   public class Src {
       @Id 
       int getId() { ... }
 
       @OneToMany(mappedBy="parent")
       Collection<Dst> getChildren() { ... }
   }

   @Entity
   public class Dst {
       @Id
       int getId() { ... }

       @ManyToOne
       Src getParent() { ... }
    }
   create table SRC (
       ID integer primary key
   )
   
   create table DST (
       ID integer primary key,

       PARENT_ID integer references(SRC, ID)
   )

[edit] Example: bidirectional @OneToMany with specified SQL

   @Entity
   @Table(name="s")
   public class Src {
       @Id 
       @Column(name="s_id")
       int getId() { ... }
 
       @OneToMany(mappedBy="parent")
       Collection<Dst> getChildren() { ... }
   }

   @Entity
   @Table(name="d")
   public class Dst {
       @Id
       @Column(name="d_id")
       int getId() { ... }

       @ManyToOne
       @JoinColumn(name="parent")
       Src getParent() { ... }
    }
   create table s (
       s_id integer primary key
   )
   
   create table d (
       d_id integer primary key

       parent integer references(s, s_id)
   )
Personal tools