- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个小型学习项目,比如测验 API。当我尝试保存复杂对象(考试)时,hibernate 抛出 EntityExistsException。我使用 dto (CreateExamRequestDto) 获取 REST 请求,然后将此 dto 映射到实体并使用 Spring Data JPA 保存它。
这是我的实体类:
考试实体:
@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-09-05 17:24:12.278 ERROR 14024 --- [nio-8080-exec-1] o.s.b.w.servlet.support.ErrorPageFilter :由于异常,从请求 [/api/exam] 转发到错误页面 [具有相同标识符值的不同对象已与 session 关联:[org.yvasylchuk.testit.domain.entity.exam.Answer#191];嵌套异常是 javax.persistence.EntityExistsException:具有相同标识符值的不同对象已与 session 关联:[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;
最佳答案
实际发生的情况:由于您正在级联答案并且由于关系是 OneToMany,因此您可能有许多超过分配大小(序列生成器)的默认值的答案具有属性分配大小,指示交易中可能增量的数量,因此如果超出限制,您的 ID 中将出现重复。
解决方案:您可以将生成器更改为自动或增加分配大小
关于java - 如何避免 javax.persistence.EntityExistsException : different object with the same identifier?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057873/
我正在尝试关注 this tutorial .我想我不是从使用可下载项目开始,而是从我之前做过的一个简单的“spring MVC - Maven - eclipse”项目开始。这个项目运行良好。 因此
我觉得 Java EE 6 规范有些困惑。有几组注释。 我们有javax.ejb注释如 @Stateful和 @Stateless用于创建 EJB。 还有一个@javax.annotation.Man
我正在开发一个依赖于“javax.lang”和“javax.annotation”的java项目。我安装了jre/jdk,类路径上还有很多其他javax.*,我每天都用Java进行开发。但是,这两个包
我正在尝试将一个值添加到 JsonValue 列表中。可以这样做吗? 一些背景知识,我正在从 Rest API 检索 Json 响应,在此 Json 中是一个如下名称列表: { “名称”:{名称1,名
我正在开发一个 JSF2、Icefaces 网络应用程序。我有以下看法: 当我保存上面的 时出现以下异常. Application caught instance of
我遇到了一个小问题,我的代码中有一个异常。 我有一个基本框架类: import java.awt.BorderLayout; import java.awt.Color; import java.aw
我正在使用 Apache Derby 并具有以下代码: DBConnectionFactory.java package edu.unsw.comp9321.jdbc; import java.sql
javax.mail 和 javax.mail-api 有什么区别? 我从 maven 存储库中找到了那些。 什么时候应该使用它们? javax.mail-api自带的软件包,但我无法使用,所以我下载
我是 Java 的新手,正在尝试进入 WebServices。我在某处找到了两个示例,但对可用选项感到困惑。 首先,带注解的javax.jws.WebService 似乎工作正常,但javax.xml
在升级了一些 Glassfish/Grizzly 依赖项之后(为了与最新版本的 Azure SDK IOT 设备客户端兼容),我开始出现错误,因为 com.google.common.EventBus
我收到了一个遗留 JSP 系统,其中 Eclipse 在每次出现以下代码时都会标记“导入 javax.event 无法解析”错误: 这是我的java版本: shakir@anduril:~$ jav
我使用以下代码获取连接到系统的 USB 设备的制造商代码。我添加了 jsr80-1.0.1 jar 。我收到以下错误 javax.usb.UsbException: Properties file j
我正在学习 EJB,当尝试使用 junit 测试它时,出现以下错误 cd.espoirmur.Ejb.InterfaceEjbLocal_80488159 Jun 03, 2016 10:33:58
我必须处理一个需要提供数据源作为参数的 API。问题是我从提供 EntityManager 或 PersistentContext 的上下文访问它,它们似乎没有通过其方法公开任何数据源。如何以编程方式
据我所知,Java ServletContext 和上下文对象在 Java EE 程序开发中很常见。但是,我不太确定它们之间有什么区别,尤其是上下文类的用法。 据我了解,ServletContext
我在一个简单的 tomcat jsp 项目中遇到了这个错误。我在谷歌上阅读的文章暗示我应该在我的项目中包含 servlet-api.jar。我就是这样做的,但没有用。有人对此有任何想法吗?我的 JRE
我想对我的实体进行一些 Bean 验证,以便我可以适本地映射错误以发送回客户端。 我知道 javax.validation.constraints 注释用于实现此目的。我的问题是,对于模式生成,我需要
想法:2018.3 jetty :9.4.11 我按照 IDEA 中给出的说明下载了 jrebel 文件夹 在 Debug模式下运行 Jetty 服务器时出现以下错误。 HTTP ERROR 500
我正在开发一个主要使用无状态 session bean (SLSB) 的 EJB3 应用程序。他们使用容器管理事务 (CMT)。 我希望 bean 知道事务(用于日志记录等)。我可以实现 javax.
我正在使用 OSGI 开发 Web 应用程序。我有一个我找不到的 Maven 配置错误。这是完整的源代码: http://uploading.com/files/8e5c9888/SH_27_test
我是一名优秀的程序员,十分优秀!