9 Replies Latest reply on Sep 25, 2006 9:47 AM by Swen Barth

    Bidirectional one-to-many resulting in multiple (identical)

    Steve Bement Newbie

      I have 3 objects, in a hierarchical relationship; bascially a two-deep one-to-many association. Group -> Display -> Component, where a Group has a one-to-many with Display, and Display has a one-to-many with Component. What's happening is when I do an em.find(), with the ID of a Group, the resulting Group comes back with multiple Displays, where there should only be one (according to the database). The pattern is, if there are 3 Components, I get three Displays back (with identical IDs). If there are 5 Components, I get 5 Displays back in the Group, and so on.

      The annotations are shown below. I've played with changing the FetchTypes, but the behavior is always the same. Also, as a workaround, I changed the Lists to Sets, so that no duplicates could be added, and that fixed it, but I don't think that should be necessary.

      Is this a known bug?

      @Entity
      public class Group {
      ...
      @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "group")
      public List getDisplays() {...}
      }

      @Entity
      public class Display {
      ...
      @JoinColumn(name="group_id")
      @ManyToOne(targetEntity = Group.class)
      public Group getGroup() {...}

      @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "display")
      public List getComponents() {...}
      }

      @Entity
      public class Component {
      ...
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(name = "display_id")
      public Child getDisplay() {...}
      }