【发布时间】:2018-08-15 14:26:48
【问题描述】:
我在使用 Spring data JPA(使用 Hibernate、Postgre SQL 和 jdbc)时遇到了问题。 我有 2 个实体,Texte 和 Annotation,以及 2 个扩展 CrudRepository 的存储库,TexteRepository 和 AnnotationRepository。 Annotation 与 Texte 具有多对一的关系。在控制器中,我正在做这样的事情:
Annotation annotation = new Annotation();
Texte texte = texteRepository.findOne(id);
if(texte != null) {
annotation.setTexte(texte);
annotationRepository.save(annotation);
}
在执行此操作时,我遇到了 ConstraintViolationException :
could not execute statement; SQL [n/a]; constraint [id_texte]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
我的理解是 Hibernate 不知道我的 texte 实体已经被持久化,所以它会尝试插入它。我来自 .NET,但我想说的是 texte 没有“附加”到同一个数据库上下文而不是注释。
这是我的实体声明(我只向您展示相关属性):
@Entity
@Getter
@Setter
public class Texte {
@Id
@GeneratedValue
private Long id;
...
}
@Entity
@Getter
@Setter
@Audited
public class Annotation {
@Id
@GeneratedValue
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@Audited(targetAuditMode = RelationTargetAuditMode.NOT_AUDITED)
private Texte texte;
...
}
非常感谢
克莱门特
【问题讨论】:
标签: spring hibernate spring-data-jpa