- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
您好,我目前正在开发小应用程序来练习 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/
我有这些 Pojo: public class ParticipantPojo implements Serializable ..... @OneToMany(mappedBy = "partici
使用 Hibernate 5 作为 ORM。 我过去认为 optional=true 意味着该列可以保存空值。 我有两个实体 -: 选修 选修实践 选修课可能会或可能不会与选修实践相关联,但选修实践必
通过执行 mvn clean package,我看到一个奇怪的 Maven 输出: [ERROR] /home/user/Desktop/mycompany/myapp/server/src/main
您好,我正在尝试保留我的一个实体“角色”,它通过连接表与我的另一个实体“用户”具有多对一关系。但是,每次我尝试在没有附加任何用户的情况下插入角色时,都会收到以下错误: org.hibernate.Pr
我收到以下异常。 NHibernate.PropertyValueException:非空属性引用空值或 transient 这是我的映射文件。 产品
我是 JPA 新手,在尝试保存事务数据时遇到了上述错误。 这是我的实体: @Entity @Table(name = "transaction1") @NamedQueries({ @
我想将一个对象保存到我的子类 ArticleZoning 中,其父类(super class) Zoning 包含类 zoneData 列表,其中还包含类 ZoneCoordinate。当我保存我的子
您好,我目前正在开发小应用程序来练习 hibernate 。我正在尝试建立一对多关系,但我陷入困境。 我有 2 门类(class)的问题和答案: package com.sample; import
Here you can see the entity-relationship diagram of my database with associations 我正在 NetBeans 中使用 A
我对 hibernate 和 restSpring 很陌生 我已经使用 hibernate 工具从数据库生成了Country和State实体 我的数据库看起来像这样国家表,country_id为PK
我是一名优秀的程序员,十分优秀!