gpt4 book ai didi

java - JPA - 可选 = true 仍然给出 PropertyValueException

转载 作者:行者123 更新时间:2023-12-02 04:14:59 25 4
gpt4 key购买 nike

使用 Hibernate 5 作为 ORM。

我过去认为 optional=true 意味着该列可以保存空值。

我有两个实体 -:

  • 选修
  • 选修实践

选修课可能会或可能不会与选修实践相关联,但选修实践必须始终与选修类(class)相关联。

选修.java

public class Elective extends Theory {

private static final long serialVersionUID = 1L;

@Column (name = "ELEC_NO",
table = "ELECTIVES",
length = 3)
private String electiveNo;

@OneToOne (targetEntity = ElectivePractical.class,
mappedBy = "elective",
fetch = FetchType.EAGER,
cascade = CascadeType.ALL,
optional = true)
private ElectivePractical practical;

@ManyToMany(targetEntity = Student.class,
fetch = FetchType.EAGER,
mappedBy = "electives")
private Collection<Student> students = new ArrayList<>();

ElectivePractical.java

public class ElectivePractical extends Practical {

private static final long serialVersionUID = 1L;

@OneToOne (targetEntity = Elective.class,
fetch = FetchType.EAGER,
cascade = CascadeType.MERGE)
@JoinTable (name = "ELECTIVE_PRACTICAL",
joinColumns = @JoinColumn (name = "ELEC_PRAC_ID",
referencedColumnName = "ID", nullable = false),
inverseJoinColumns =
@JoinColumn (name = "ELEC_THEORY_ID",
referencedColumnName = "ID", nullable = false))
private Elective elective;

如您所见,Elective.java 中的 ElectivePractical 设置为 optional=true

然而,当我尝试 merge() 一个没有实际意义的 Elective 实例时,我收到以下错误 -:

javax.persistence.PersistenceException: org.hibernate.PropertyValueException: not-null property references a null or transient value : com.sts.entity.subjects.Elective.practical
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1692)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1602)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1608)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1171)
at com.sts.persistence.STSDao.addCourse(STSDao.java:250)
at com.sts.validators.CourseValidator.commit(CourseValidator.java:558)
at test.main.CourseTest.main(CourseTest.java:27)
Caused by: org.hibernate.PropertyValueException: not-null property references a null or transient value : com.sts.entity.subjects.Elective.practical
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:92)
at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:115)
at org.hibernate.action.internal.AbstractEntityInsertAction.makeEntityManaged(AbstractEntityInsertAction.java:124)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:185)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:163)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:150)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:325)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:272)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:178)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:121)
at org.hibernate.jpa.event.internal.core.JpaMergeEventListener.saveWithGeneratedId(JpaMergeEventListener.java:56)
at org.hibernate.event.internal.DefaultMergeEventListener.saveTransientEntity(DefaultMergeEventListener.java:255)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsTransient(DefaultMergeEventListener.java:235)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:173)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:69)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:839)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:821)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:826)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1161)
... 3 more

我做错了什么?这是 hibernate 中的错误吗?

最佳答案

option=true 元素仅用于生成 DDL 以及验证架构。如果您手动创建了数据库,则实际的数据库列很可能是 NOT NULL,即使您的 Hibernate 映射表示相反。

顺便说一句,您的映射可以精简,例如,我认为您不需要使用较新版本的 Hibernate 的 targetEntity

更新:

我发现您在与 optional=true 元素同一侧使用 mappedBymappedBy 元素告诉 Hibernate 关系的另一端包含实际的映射。这可能包括 optional=true 元素,因此它被忽略。尝试将其添加到关系的另一方。

关于java - JPA - 可选 = true 仍然给出 PropertyValueException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33412645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com