gpt4 book ai didi

java - Hibernate TransientPropertyValueException 在保存数据时

转载 作者:搜寻专家 更新时间:2023-11-01 02:23:03 25 4
gpt4 key购买 nike

我正在尝试使用 hibernate 将数据插入数据库。以下是我将如何执行该操作

    session.beginTransaction();
pojo.StuDetails stu = new StuDetails();
stu.setFName(f_name);
stu.setLName(l_name);
stu.setSex(sex);
stu.setDob(dob);

pojo.Subject sub = new Subject(subject, day, time);
pojo.SubjectHasStuDetails shs = new SubjectHasStuDetails(stu, sub);

session.save(shs);
session.getTransaction().commit();

但是它给了我一个错误提示

Exception in thread "main" org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation

这是我的学生详细信息实体

 public class StuDetails  implements java.io.Serializable {


private Integer id;
private String FName;
private String LName;
private String sex;
private String dob;
private Set subjectHasStuDetailses = new HashSet();
...
//constructors and getters, setters

我的 StudentDetails hbm.xml

<hibernate-mapping>
<class name="pojo.StuDetails" table="stu_details" catalog="laravel_test" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="FName" type="string">
<column name="f_name" length="45" not-null="true" />
</property>
<property name="LName" type="string">
<column name="l_name" length="45" not-null="true" />
</property>
<property name="sex" type="string">
<column name="sex" length="45" not-null="true" />
</property>
<property name="dob" type="string">
<column name="dob" length="45" not-null="true" />
</property>
<set name="subjectHasStuDetailses" table="subject_has_stu_details" inverse="true" lazy="true" fetch="select">
<key>
<column name="stu_details_id" not-null="true" />
</key>
<one-to-many class="pojo.SubjectHasStuDetails" />
</set>
</class>
</hibernate-mapping>

我的主题实体看起来像

 public class Subject  implements java.io.Serializable {


private Integer id;
private String subName;
private String day;
private String time;
private Set subjectHasStuDetailses = new HashSet();

...
//constructors and getters, setters

主题.hbm.xml

<hibernate-mapping>
<class name="pojo.Subject" table="subject" catalog="laravel_test" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<property name="subName" type="string">
<column name="sub_name" length="45" not-null="true" />
</property>
<property name="day" type="string">
<column name="day" length="45" not-null="true" />
</property>
<property name="time" type="string">
<column name="time" length="45" not-null="true" />
</property>
<set name="subjectHasStuDetailses" table="subject_has_stu_details" inverse="true" lazy="true" fetch="select">
<key>
<column name="subject_id" not-null="true" />
</key>
<one-to-many class="pojo.SubjectHasStuDetails" />
</set>

</class>
</hibernate-mapping>

这是 SubjetcHasStuDetails 实体

 public class SubjectHasStuDetails  implements java.io.Serializable {


private Integer id;
private StuDetails stuDetails;
private Subject subject;
...
//constructors and getters, setters

SubjectHasStuDetials.hbm.xml

<hibernate-mapping>
<class name="pojo.SubjectHasStuDetails" table="subject_has_stu_details"
catalog="laravel_test" optimistic-lock="version">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="stuDetails" class="pojo.StuDetails" fetch="select">
<column name="stu_details_id" not-null="true" />
</many-to-one>
<many-to-one name="subject" class="pojo.Subject" fetch="select" >
<column name="subject_id" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>

请有人帮我解决这个错误...谢谢..

最佳答案

在您的 SubjectHasStuDetials.hbm.xml 进行这些更改:

<many-to-one name="stuDetails" class="pojo.StuDetails" fetch="select" cascade="all">
<column name="stu_details_id" not-null="true" />
</many-to-one>
<many-to-one name="subject" class="pojo.Subject" fetch="select" cascade="all" >
<column name="subject_id" not-null="true" />
</many-to-one>

stuDetailssubject 多对一标签添加 cascade="all" 属性。

  • Cascade attribute 是强制性的,当我们应用关系时在对象之间,级联属性传输在一个对象上完成的操作对象到其相关的子对象上
  • 如果我们写cascade = “all” 那么父类对象的改变将会也会影响到子类对象,如果我们写 cascade = “all”那么父对象的所有操作,如插入、删除、更新都将也会对子对象产生影响。
  • 示例:如果我们对父级应用插入(或更新或删除)操作类对象,则子类对象也将存储到数据库。

关于java - Hibernate TransientPropertyValueException 在保存数据时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33872396/

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