3 Replies Latest reply on Feb 23, 2006 7:35 AM by cspada

    @OneToMany relation issues

    cspada

      Hi,
      me again.

      A have some issues with an unidirectional @OneToMany relation
      Here are the code and results :

      //////////////////////////////////////////////////// Entity bean

      @Entity
      @Table(name = "MANAGER")
      public class Manager implements Serializable {
      protected Collection managedFleets;

      /**
      * Get the managedFleets.
      * @return Returns the managedFleets.
      */
      @OneToMany(fetch = FetchType.LAZY)
      @JoinTable(name = "FLEET_MGR",
      joinColumns = { @JoinColumn(name = "MGR_ID") },
      inverseJoinColumns = { @JoinColumn(name = "FLEET_ID") })
      public Collection getManagedFleets() {
      return managedFleets;
      }

      /**
      * Set the managedFleets.
      * @param managedFleets The managedFleets to set.
      */
      public void setManagedFleets(Collection managedFleets) {
      this.managedFleets = managedFleets;
      }
      }



      /////////////////////////////////////////////////// Session bean


      public void assignFleetToManager(Integer fleetId, Integer managerId) {
      Fleet fleet = em.find(Fleet.class, fleetId);
      Manager manager = em.find(Manager.class, managerId);
      if ((fleet != null) && (manager != null)) {
      managedFleets.add(fleet);
      }
      }

      A call to this method on a FLEET already associated to a MANAGER creates
      a new association and does not delete the OLD association. This leads to
      a fleet assigned to 2 managers which is no more a OneToMany relation...

      BEFORE:
      MGR_ID FLEET_ID
      107 5
      108 6

      AFTER a call to assignFleetToManager(6, 107)
      MGR_ID FLEET_ID
      107 5
      107 6
      108 6


      Another issue is that if a call twice the assignFleetToManager method,
      jboss does not notice that the relation still exists and insert a new one
      that produce a SQL exception cause of ducplicate primy keys.

      What did i miss ?
      Thank you
      Christophe.

        • 1. Re: @OneToMany relation issues
          cspada

          No one to help on this ?

          • 2. Re: @OneToMany relation issues
            wereldkeuken

            I don't see the reason why you use @JoinTable for a onetomany relationship? This is meant for a manytomany relationship no?

            • 3. Re: @OneToMany relation issues
              cspada

               

              "wereldkeuken" wrote:
              I don't see the reason why you use @JoinTable for a onetomany relationship? This is meant for a manytomany relationship no?


              Yes it is. But i am actually coding my EJB3 code for an application, but i have to use the old Datamodel in place.
              So i have no choice for the moment.

              I have to deal with a join table to simulate a @ManyToOne relation.
              I'm working on this since yesterday and can't find a working solution.

              So i need help on this...