title: Hibernate: saves object twice into table when connected with an eagerloaded OneToMany
body: <p>the class rideSingle is always saved multiple times into the db if a OneToMany relationship is set to eager:</p>
  
  <p>The class which is saved</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  public class RideSingle  extends HibernateBasicClass{
      @OneToMany(mappedBy = "rideSingle", fetch = FetchType.EAGER)
      @Cascade(CascadeType.ALL)
      protected List&lt;TimedRouteLocation&gt; route;
  }
  </code></pre>
  
  <p>The refered class:</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  public class TimedRouteLocation  extends HibernateBasicClass {
  
      @ManyToOne(fetch = FetchType.LAZY)
      @JoinColumn(nullable = false)
      protected RideSingle rideSingle;
  }
  </code></pre>
  
  <p>the hibernate base class</p>
  
  <pre><code>@Entity
  @Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
  public abstract class HibernateBasicClass implements Serializable {
      private static final long serialVersionUID = -2253382207065396453L;
  
      @Id
      @SequenceGenerator(name = "generator", allocationSize = 1, initialValue = 1)
      @GeneratedValue(strategy = GenerationType.TABLE, generator = "generator")
      @JsonProperty
      private Long id;
  
      @Column(nullable = false)
      @Temporal(TemporalType.TIMESTAMP)
      private Date createdOn = new Date();
  
      @Version
      private Date lastModified;
  
      public HibernateBasicClass() {
          super();
      }
  
      public Long getId() {
          return id;
      }
  
      @Override
      public boolean equals(Object obj) {
          if (this == obj)
              return true;
          if (obj == null)
              return false;
          if (getClass() != obj.getClass())
              return false;
          final HibernateBasicClass objWId = (HibernateBasicClass) obj;
          // same id instance
          if (getId() == objWId.getId())
              ;
          // same id content
          if (getId() != null) {
              return getId().equals(objWId.getId());
          } else {
              return false;
          }
      }
  
  }
  </code></pre>
  
  <p>There is just one variable, which then is saved by</p>
  
  <pre><code>    int i = rideSingleDaox.findAll().size(); // i is 0
      rideSingleDao.saveOrUpdate(this);
      i = rideSingleDaox.findAll().size(); // i is 2
  </code></pre>
  
  <p>The Dao looks like this:</p>
  
  <pre><code>public T saveOrUpdate(T object) {
      getSession().saveOrUpdate(object);
              return object;
  }
  </code></pre>
  
  <p>If the rideSingle.route has 2 elements -> rideSingle is put twice in the db. If route has 3 elements, 3 copies of rideSingle are put in the database...</p>
  
  <p>If rideSingle loads the route lazy, this problem does not occure.</p>
  
  <pre><code>@Entity
  public class RideSingle {
      @OneToMany(mappedBy = "rideSingle", fetch = FetchType.EAGER)
      @Cascade(CascadeType.ALL)
      protected List&lt;TimedRouteLocation&gt; route;
  }
  </code></pre>
  
  <p>Does anybody know why this is the case and how i could solve it?</p>
  
  <p>Thanks a lot!</p>