gpt4 book ai didi

java - Hibernate异常org.hibernate.PropertyValueException : not-null property references a null or transient value

转载 作者:太空宇宙 更新时间:2023-11-04 14:04:10 24 4
gpt4 key购买 nike

您好,我目前正在开发小应用程序来练习 hibernate 。我正在尝试建立一对多关系,但我陷入困境。

我有 2 门类(class)的问题和答案:

package com.sample;

import java.util.List;

public class Question {
private int id;
private String qname;
private List<Answer> answers;

public Question() {

}

public Question(int id, String qname, List<Answer> answers) {
super();
this.id = id;
this.qname = qname;
this.answers = answers;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getQname() {
return qname;
}

public void setQname(String qname) {
this.qname = qname;
}

public List<Answer> getAnswers() {
return answers;
}

public void setAnswers(List<Answer> answers) {
this.answers = answers;
}

}

package com.sample;

public class Answer {
private int id;
private String answername;
private String postedBy;
private Question question;

public Answer() {

}

public Answer(int id, String answername, String postedBy, Question question) {
super();
this.id = id;
this.answername = answername;
this.postedBy = postedBy;
this.question = question;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getAnswername() {
return answername;
}

public void setAnswername(String answername) {
this.answername = answername;
}

public String getPostedBy() {
return postedBy;
}

public void setPostedBy(String postedBy) {
this.postedBy = postedBy;
}

public Question getQuestion() {
return question;
}

public void setQuestion(Question question) {
this.question = question;
}


}

配置文件如下:

问题.hbm.xml:

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.sample.Question" table="question">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="qname"></property>

<bag name="answers" table="answer" inverse="true" lazy="true"
fetch="select" cascade="all">
<key>
<column name="question_id" not-null="true">
</column>
</key>
<one-to-many class="com.sample.Answer"></one-to-many>
</bag>

</class>

</hibernate-mapping>

answer.hbm.xml:

<?xml version='1.0' encoding='UTF-8'?>  
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name="com.sample.Answer" table="answer">
<id name="id">
<generator class="increment"></generator>
</id>
<property name="answername"></property>
<property name="postedBy"></property>
<many-to-one name="question" class="com.sample.Question"
fetch="select">
<column name="question_id" not-null="true">
</column>
</many-to-one>
</class>

</hibernate-mapping>

我的主文件如下所示:

import java.util.ArrayList;

import org.hibernate.*;
import org.hibernate.cfg.*;

public class StoreData {
public static void main(String[] args) {
Session session = SessionFactoryUtil.getSessionFactory()
.getCurrentSession();
Transaction t = session.beginTransaction();

Answer ans1 = new Answer();
ans1.setAnswername("java is a programming language");
ans1.setPostedBy("Ravi Malik");

Answer ans2 = new Answer();
ans2.setAnswername("java is a platform");
ans2.setPostedBy("Sudhir Kumar");

Answer ans3 = new Answer();
ans3.setAnswername("Servlet is an Interface");
ans3.setPostedBy("Jai Kumar");

Answer ans4 = new Answer();
ans4.setAnswername("Servlet is an API");
ans4.setPostedBy("Arun");

ArrayList<Answer> list1 = new ArrayList<Answer>();
list1.add(ans1);
list1.add(ans2);

ArrayList<Answer> list2 = new ArrayList<Answer>();
list2.add(ans3);
list2.add(ans4);

Question question1 = new Question();
question1.setQname("What is Java?");
question1.setAnswers(list1);

Question question2 = new Question();
question2.setQname("What is Servlet?");
question2.setAnswers(list2);

session.persist(question1);
session.persist(question2);

t.commit();
// session.close();
System.out.println("success");

}
}

当我运行该应用程序时,出现以下错误:

Exception in thread "main" org.hibernate.PropertyValueException: not-null property references a null or transient value : com.sample.Answer.question
at org.hibernate.engine.internal.Nullability.checkNullability(Nullability.java:106)
at org.hibernate.action.internal.AbstractEntityInsertAction.nullifyTransientReferencesIfNotAlready(AbstractEntityInsertAction.java:132)
at org.hibernate.action.internal.AbstractEntityInsertAction.makeEntityManaged(AbstractEntityInsertAction.java:141)
at org.hibernate.engine.spi.ActionQueue.addResolvedEntityInsertAction(ActionQueue.java:203)
at org.hibernate.engine.spi.ActionQueue.addInsertAction(ActionQueue.java:181)
at org.hibernate.engine.spi.ActionQueue.addAction(ActionQueue.java:168)
at org.hibernate.event.internal.AbstractSaveEventListener.addInsertAction(AbstractSaveEventListener.java:332)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:288)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:137)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:801)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:794)
at org.hibernate.engine.spi.CascadingActions$7.cascade(CascadingActions.java:314)
at org.hibernate.engine.internal.Cascade.cascadeToOne(Cascade.java:350)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:293)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
at org.hibernate.engine.internal.Cascade.cascadeCollectionElements(Cascade.java:379)
at org.hibernate.engine.internal.Cascade.cascadeCollection(Cascade.java:319)
at org.hibernate.engine.internal.Cascade.cascadeAssociation(Cascade.java:296)
at org.hibernate.engine.internal.Cascade.cascadeProperty(Cascade.java:161)
at org.hibernate.engine.internal.Cascade.cascade(Cascade.java:118)
at org.hibernate.event.internal.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:460)
at org.hibernate.event.internal.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:294)
at org.hibernate.event.internal.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:194)
at org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:137)
at org.hibernate.event.internal.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:206)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:149)
at org.hibernate.event.internal.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:75)
at org.hibernate.internal.SessionImpl.firePersist(SessionImpl.java:811)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:784)
at org.hibernate.internal.SessionImpl.persist(SessionImpl.java:789)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:356)
at com.sun.proxy.$Proxy0.persist(Unknown Source)
at com.sample.StoreData.main(StoreData.java:46)

最佳答案

Hibernate 尝试保存引用问题的答案,但保存时没有 id。

首先尝试保存没有答案的问题,然后为每个答案调用 setQuestion 传递保存的问题。只需保存答案即可。

关于java - Hibernate异常org.hibernate.PropertyValueException : not-null property references a null or transient value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29033251/

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