title: A different object with the same identifier value was already associated
body: <p>in my Java project, i have these three class and a code to insert data:</p>
  
  <pre><code>@Entity(name = "Quiz")
  @Table(name = "quiz")
  public class Quiz implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
  
      private String name;
  
      @ManyToOne(cascade= CascadeType.ALL)
      private Skill skill;
  
      @ManyToMany(fetch = FetchType.EAGER, cascade=CascadeType.ALL)
      private List&lt;StudentClass&gt; stClasses= new ArrayList&lt;&gt;();
  
  
  @Entity
  public class Skill implements Serializable {
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String name;
  
  
  @Entity
  @Table(name = "Student_Class")
  @PrimaryKeyJoinColumn(name="id")
  public class StudentClass implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Long id;
      private String name;
  
      @OneToMany(fetch= FetchType.EAGER, cascade = { CascadeType.ALL })
      @JoinColumn(name = "stClass_id", referencedColumnName="id")
      private Set&lt;User&gt; students=new HashSet&lt;&gt;();
  
      @ManyToMany(fetch= FetchType.EAGER, mappedBy="stClasses", cascade=CascadeType.ALL)
      private Set&lt;Quiz&gt; quizzes= new HashSet&lt;&gt;();
  
  @Transactional
  public void save(Quiz quiz) {
      hibernateTemplate.saveOrUpdate(quiz);
  }
  </code></pre>
  
  <p>but if I insert a Quiz with a StudentClass that already uses a Skill I get this error:</p>
  
  <pre><code>GRAVE: Servlet.service() for servlet [appServlet] in context with path [/AutoQuiz3000] threw exception [Request processing failed; nested exception is org.springframework.dao.DuplicateKeyException: A different object with the same identifier value was already associated with the session : [fr.dawan.autoquiz3000.beans.Skill#10]; nested exception is org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [fr.dawan.autoquiz3000.beans.Skill#10]] with root cause
  org.hibernate.NonUniqueObjectException: A different object with the same identifier value was already associated with the session : [fr.dawan.autoquiz3000.beans.Skill#10]
  at org.hibernate.engine.internal.StatefulPersistenceContext.checkUniqueness(StatefulPersistenceContext.java:648)
  at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performUpdate(DefaultSaveOrUpdateEventListener.java:284)
  at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsDetached(DefaultSaveOrUpdateEventListener.java:227)
  at org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:92)
  </code></pre>
  
  <p>example:</p>
  
  <pre><code>quiz.setName("what the capital...");
  quiz.setSkill("geography");
  quiz.addstClass("formation Java");
  
  quiz.setName("what the money...");
  quiz.setSkill("geography");
  quiz.addstClass("formation Java");
  </code></pre>
  
  <p>I have an error because the StudentClass is already using Skill! if I change the StudentClass I have not problem.</p>
  
  <p>do not know how come the problem! someone an idea?</p>
  
  <p>Thank you so much!</p>