1 Reply Latest reply on Feb 22, 2006 6:18 AM by leenamba

    How to make a joint by condition ?

    Taud Anas Newbie

      I have an entity witch map with PERSON table

      @Entity
      @Table(name = "PERSON")
      public class PersonEntity implements Serializable {

      protected static final long serialVersionUID = 1L;

      protected int numPerson;
      protected String sex;
      ...
      protected Collection childs;


      @Id( generate = GeneratorType.AUTO)
      @Column (name = "NUM_PERSON")
      public int getNumPerson() {
      return numPerson;
      }

      public void setNumPerson(int numPerson) {
      this.numPerson = numPerson;
      }

      @Column (name = "SEX")
      public String getSex() {
      return sex;
      }

      public void setSex(String sex) {
      this.sex = sex;
      }

      ..

      @OneToMany(cascade=CascadeType.ALL, fetch=FetchType.LAZY, mappedBy="father")
      @JoinColumn(name="FK_PERSON_FATHER") // Foreign key in PERSON
      public Collection getChilds() {
      return childs;
      }

      public void setChilds(Collection childs) {
      this.childs = childs;
      }
      }

      My problem is:
      I need to load "childs" collection by joint on FK_PERSON_FATHER column if SEX='M' or by joint on FK_PERSON_MOTHER column if SEX='W'.
      (In the exemple above the joint is done on FK_PERSON_FATHER)

      Is it possible to do that ?

      here is the DDL of "PERSON" table

      CREATE TABLE PERSON (

      NUM_PERSON INTEGER AUTO_INCREMENT NOT NULL
      ,SEX VARCHAR(1) NOT NULL

      ,FK_PERSON_FATHER INTEGER
      ,FK_PERSON_MOTHER INTEGER
      ,PRIMARY KEY (NUM_PERSON)
      );

      INSERT INTOPERSON (NUM_PERSON,SEX) VALUES (1,'racin','racin','racin','racin','M');

      ALTER TABLE PERSON ADD (
      FOREIGN KEY (FK_PERSON_FATHER) REFERENCES PERSON(NUM_PERSON)
      ,FOREIGN KEY (FK_PERSON_MOTHER) REFERENCES PERSON(NUM_PERSON)
      );