我有一個小問題項目,就像測驗API一樣。當我嘗試保存複雜對象(考試)時,hibernate拋出EntityExistsException。我使用dto(CreateExamRequestDto)獲得REST請求,然後將此dto映射到實體並使用Spring Data JPA進行保存。如何避免javax.persistence.EntityExistsException:具有相同標識符的不同對象?
這裏是我的實體類:
EXAM實體:
@Entity
@Table(name = "exams", uniqueConstraints = {@UniqueConstraint(columnNames = {
"name",
"category_id"
})})
public class Exam extends AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "exams_seq_gen")
@SequenceGenerator(name = "exams_seq_gen", sequenceName = "exams_id_seq")
@Column(name = "id")
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@ManyToOne
@JoinColumn(name = "category_id", nullable = false)
private Category category;
@ManyToOne
@JoinColumn(name = "owner_id", nullable = false)
private User owner;
@OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private List<Section> sections;
@Column(name = "target_score", nullable = false)
private Integer targetScore;
@OneToMany()
private List<ExamResult> results;
@Transient
private ExamResult result;
//public constructor, getters, setters, hashcode and equals
}
部分實體:
@Entity
@Table(name = "exam_sections", uniqueConstraints = @UniqueConstraint(columnNames = {
"name",
"exam_id"
}))
public class Section extends AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "exam_sections_seq_gen")
@SequenceGenerator(name = "exam_sections_seq_gen", sequenceName = "exam_sections_id_seq")
@Column(name = "id")
private Integer id;
@Column(name = "name", nullable = false)
private String name;
@Column(name = "question_weight", nullable = false)
private Integer questionWeight;
@Column(name = "target_questions", nullable = false)
private Integer targetQuestions;
@OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private List<Question> questions;
@ManyToOne
@JoinColumn(name = "exam_id", nullable = false)
private Exam exam;
//public constructor, getters, setters, hashcode and equals
}
問題實體:
@Entity
@Table(name = "questions")
public class Question extends AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "questions_seq_gen")
@SequenceGenerator(name = "questions_seq_gen", sequenceName = "questions_id_seq")
@Column(name = "id")
private Integer id;
@Column(name = "text", nullable = false)
private String text;
@Column(name = "type", nullable = false)
@Enumerated(EnumType.STRING)
private QuestionType type;
@OneToMany(cascade = {CascadeType.MERGE, CascadeType.PERSIST})
private List<Answer> answers;
@ManyToOne
@JoinColumn(name = "section_id")
private Section section;
//public constructor, getters, setters, hashcode and equals
}
答案實體:
@Entity
@Table(name = "answers")
public class Answer extends AbstractEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answers_seq_gen")
@SequenceGenerator(name = "answers_seq_gen", sequenceName = "answers_id_seq")
@Column(name = "id")
private Integer id;
@Column(name = "text", nullable = false)
private String text;
@Column(name = "correct", nullable = false)
private Boolean correct;
@ManyToOne
@JoinColumn(name = "question_id", nullable = false)
private Question question;
//public constructor, getters, setters, hashcode and equals
}
和代碼保存了一切:
@Override
@Transactional
public ExamShortDto createExam(CreateExamRequestDto request) {
Exam exam = examMapper.mapToEntity(request, user);
exam = examRepository.save(exam); <<----- HERE
return examMapper.mapToShortDto(exam);
}
在尖線我有一個異常:
2017年9月5日17:24:12.278錯誤14024 --- [nio-8080-exec-1] osbwservlet.support.ErrorPageFilter:由於異常[由於請求[/ api/exam]轉發到錯誤頁面[具有相同標識符值的不同對象已與會話相關聯:[org。 yvasylchuk.testit.domain.entity.exam.Answer#191]。嵌套異常是javax.persistence.EntityExistsException:用相同的標識符值的不同的對象已經與會話相關聯:[org.yvasylchuk.testit.domain.entity.exam.Answer#191]]
org.springframework.dao.DataIntegrityViolationException: A different object with the same identifier value was already associated with the session : [org.yvasylchuk.testit.domain.entity.exam.Answer#191]; nested exception is javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [org.yvasylchuk.testit.domain.entity.exam.Answer#191]
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:395) ~[spring-orm-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:226) ~[spring-orm-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:499) ~[spring-orm-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59) ~[spring-tx-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:216) ~[spring-tx-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) ~[spring-tx-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:134) ~[spring-data-jpa-2.0.0.RC2.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) ~[spring-aop-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61) ~[spring-data-commons-2.0.0.RC2.jar:na]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.0.RC3.jar:5.0.0.RC3]
at com.sun.proxy.$Proxy130.save(Unknown Source) ~[na:na]
at org.yvasylchuk.testit.service.exam.impl.ExamFacadeImpl.createExam(ExamFacadeImpl.java:143) ~[classes/:0.0.1-SNAPSHOT]
......
Caused by: javax.persistence.EntityExistsException: A different object with the same identifier value was already associated with the session : [org.yvasylchuk.testit.domain.entity.exam.Answer#191]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:116) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:155) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.ExceptionConverterImpl.convert(ExceptionConverterImpl.java:162) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:771) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:458) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:383) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:491) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:423) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:386) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:126) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:445) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:281) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:804) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:771) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:458) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:383) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:491) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:423) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:386) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:126) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:445) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:281) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:804) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:771) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener$1.cascade(JpaPersistEventListener.java:80) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:458) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:383) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:491) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:423) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:386) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:193) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:126) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:445) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:281) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:182) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:125) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.jpa.event.internal.core.JpaPersistEventListener.saveWithGeneratedId(JpaPersistEventListener.java:67) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:189) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:132) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:58) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:780) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:765) ~[hibernate-core-5.2.10.Final.jar:5.2.10.Final]
...
解:
我只是在回答實體從改變一個行:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answers_seq_gen")
HERE -->> @SequenceGenerator(name = "answers_seq_gen", sequenceName = "answers_id_seq")
@Column(name = "id")
private Integer id;
到:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "answers_seq_gen")
HERE -->> @SequenceGenerator(name = "answers_seq_gen", sequenceName = "answers_id_seq", allocationSize = 200)
@Column(name = "id")
private Integer id;
做你'id'(S)列在你的數據庫已經啓用AUTO_INCREMENT或類似的東西? – Shirkam