将Hibernate 5用作ORM。
我认为optional=true
意味着列可以保存空值。
我有两个实体 - :
选修课可能与ElectivePractical相关或不相关,但ElectivePractical必须始终与选修课相关联。
Elective.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;
您可以看到ElectivePractical
中的Elective.java
设置为optional=true
然而,当我尝试merge()
一个没有实际意义的选修实例时,我得到以下错误 - :
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中的一个错误吗?
答案 0 :(得分:2)
optional=true
元素仅用于生成DDL以及模式验证。如果您手动创建了数据库,那么即使您的Hibernate映射表明相反,实际的数据库列也可能是NOT NULL
。
顺便说一下,你的映射可以缩小,例如我觉得你不需要targetEntity
更新版本的Hibernate。
更新
我发现您在mappedBy
- 元素的同一侧使用optional=true
。 mappedBy
- 元素告诉Hibernate
关系的另一面包含实际的映射。它可能包含optional=true
- 元素,因此会被忽略。尝试将其添加到关系的另一端。