Relation catalog

From CauchoWiki

Jump to: navigation, search


Contents

[edit] @ManyToOne

The @ManyToOne relation is a unidirectional link, represented by a foreign key in the database. The @ManyToOne relation serves as the foundation of the persistent relations.

[edit] @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] @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
   )

[edit] Tutorial

@ManyToOne tutorial

[edit] @OneToMany unidirectional

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

[edit] unidirectional @OneToMany default mapping

   @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] 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] @OneToMany bidirectional

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

[edit] unidirectional @OneToMany default mapping

   @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] 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)
   )

[edit] Tutorial

@OneToMany tutorial

[edit] @ManyToMany unidirectional

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

[edit] unidirectional @ManyToMany default mapping

   @Entity
   public class Src {
       @Id 
       int getId() { ... }
 
       @ManyToMany
       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] unidirectional @ManyToMany with specified SQL

   @Entity
   @Table(name="s")
   public class Src {
       @Id 
       @Column(name="s_id")
       int getId() { ... }
 
       @ManyToMany
       @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] Tutorial

@ManyToMany tutorial

Personal tools