Hibernate TransientPropertyValueException 在保存数据时
Hibernate TransientPropertyValueException When saving data
我正在尝试使用 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
我的学生详情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>
将 cascade="all"
属性添加到 stuDetails
和 subject
多对一标签。
- Cascade 属性 是必需的,每当我们应用关系时
在对象之间,级联属性传输在一个对象上完成的操作
对象到其相关的子对象上
- 如果我们写
cascade = “all”
那么父 class 对象的变化将会
如果我们写 cascade = “all”,也会对子 class 对象产生影响
那么父对象的所有操作,如插入、删除、更新都将
也影响子对象。
- 示例:如果我们对父级应用插入(或更新或删除)操作
class 对象,那么子 class 对象也将存储到
数据库。
我正在尝试使用 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
我的学生详情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>
将 cascade="all"
属性添加到 stuDetails
和 subject
多对一标签。
- Cascade 属性 是必需的,每当我们应用关系时 在对象之间,级联属性传输在一个对象上完成的操作 对象到其相关的子对象上
- 如果我们写
cascade = “all”
那么父 class 对象的变化将会 如果我们写 cascade = “all”,也会对子 class 对象产生影响 那么父对象的所有操作,如插入、删除、更新都将 也影响子对象。 - 示例:如果我们对父级应用插入(或更新或删除)操作 class 对象,那么子 class 对象也将存储到 数据库。