в моем проекте Java у меня есть эти три класса и код для вставки данных:
@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<StudentClass> stClasses= new ArrayList<>();
@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<User> students=new HashSet<>();
@ManyToMany(fetch= FetchType.EAGER, mappedBy="stClasses", cascade=CascadeType.ALL)
private Set<Quiz> quizzes= new HashSet<>();
@Transactional
public void save(Quiz quiz) {
hibernateTemplate.saveOrUpdate(quiz);
}
но если я вставляю тест с StudentClass, который уже использует навык, я получаю эту ошибку:
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)
пример:
quiz.setName("what the capital...");
quiz.setSkill("geography");
quiz.addstClass("formation Java");
quiz.setName("what the money...");
quiz.setSkill("geography");
quiz.addstClass("formation Java");
У меня ошибка, потому что StudentClass уже использует Skill! если я изменю StudentClass, у меня нет проблем.
не знаю, как возникла проблема! У кого-то есть идея?
Большое спасибо!