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

    @OneToMany relation issues

    christophe spada Newbie

      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.