2 Replies Latest reply on May 18, 2006 2:30 PM by epbernard

    ENTITY BEAN WITH SELF RELATIONSHIP

    sierras

      I've been looking around and around for some doc about how to make a Entity Bean with a one to many self relationship. (with a self foreign key).

      I've found nothing.

      EMPLOYEE with EMPLOYEE in a "Manager OF" relationship. Want to be able to locate both direction of the relationship.

      Anyone can help me please?

      thanks.

        • 1. Re: ENTITY BEAN WITH SELF RELATIONSHIP
          tinashechipomho

          I know you can do it in one direction but not both, I haven't been able to do it in both directions

          say you have

          @Entity
          class Employee{
           employeeno;
           name;
           ....
           mangerid;
           //managerid points to an existing employeeno in the same schema
          }
          

          from the code above you can have the following relationship setup in one direction
          Employee can get a Manager (another employee) but from a Manager Employee to get a list of his subordinates (possible by running the query below) is not straight forward:
           SELECT e FROM Employee e WHERE e.managerid=:currentemployeeid
          


          to establish a self-referenced relationship try
           class Employee{
           @Id employeeno;
           name;
           //....other properties...
           @ManyToOne
           @JoinColumn(name="managerid",referencedColumn="employeeno")
           managerid
           }
          


          i have done this from memory, I will probably pull out me examples later on, and double check the @JoinColumn annotation I always get the name, and referencedname upside down

          • 2. Re: ENTITY BEAN WITH SELF RELATIONSHIP
            epbernard

            @Entity
            public class Employee {
            @Id private Integer Id
            @ManyToOne Employee manager;
            @OneToMany(mappedBy="manager") Set team;