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();
但它给了我一个错误,说:
线程main中的异常
org.hibernate.TransientPropertyValueException:非空属性
引用一个瞬态值 - 瞬态实例必须保存在
之前当前操作
这是我的学生资料实体
public class StuDetails实现了java.io.Serializable {
private Integer id;
private String FName;
private String LName;
私人字符串性别;
private String dob;
private Set subjectHasStuDetailses = new HashSet();
...
//构造函数和getter,setters
我的StudentDetails hbm .xml
< hibernate-mapping>
< class name =pojo.StuDetailstable =stu_detailscatalog =laravel_testoptimistic-lock =version>
< id name =idtype =java.lang.Integer>
< column name =id/>
< generator class =identity/>
< / id>
< property name =FNametype =string>
< column name =f_namelength =45not-null =true/>
< / property>
< property name =LNametype =string>
< column name =l_namelength =45not-null =true/>
< / property>
< property name =sextype =string>
< column name =sexlength =45not-null =true/>
< / property>
< property name =dobtype =string>
< column name =doblength =45not-null =true/>
< / property>
< set name =subjectHasStuDetailsestable =subject_has_stu_detailsinverse =truelazy =truefetch =select>
< key>
< column name =stu_details_idnot-null =true/>
< / key>
<一对多等级=pojo.SubjectHasStuDetails/>
< / set>
< / class>
< / hibernate-mapping>
My Subject Entity看起来像
private Integer id;
private String subName;
私人字符串日;
私人字符串时间;
private Set subjectHasStuDetailses = new HashSet();
...
//构造函数和getter,setters
Subject.hbm.xml
< hibernate-mapping>
< class name =pojo.Subjecttable =subjectcatalog =laravel_testoptimistic-lock =version>
< id name =idtype =java.lang.Integer>
< column name =id/>
< generator class =identity/>
< / id>
< property name =subNametype =string>
< column name =sub_namelength =45not-null =true/>
< / property>
< property name =daytype =string>
< column name =daylength =45not-null =true/>
< / property>
< property name =timetype =string>
< column name =timelength =45not-null =true/>
< / property>
< set name =subjectHasStuDetailsestable =subject_has_stu_detailsinverse =truelazy =truefetch =select>
< key>
< column name =subject_idnot-null =true/>
< / key>
<一对多等级=pojo.SubjectHasStuDetails/>
< / set>
< / class>
< / hibernate-mapping>
以下是SubjetcHasStuDetails实体
private Integer id;
私人StuDetails stuDetails;
私人主题;
...
//构造函数和getter,setters
SubjectHasStuDetials.hbm .xml
< hibernate-mapping>
< class name =pojo.SubjectHasStuDetailstable =subject_has_stu_details
catalog =laravel_testoptimistic-lock =version>
< id name =idtype =java.lang.Integer>
< column name =id/>
< generator class =identity/>
< / id>
<多对一名称=stuDetailsclass =pojo.StuDetailsfetch =select>
< column name =stu_details_idnot-null =true/>
< /多对一>
< column name =subject_idnot-null =true/>
< /多对一>
< / class>
< / hibernate-mapping>
有人可以帮我解决这个错误,谢谢..
$ b $在您的 SubjectHasStuDetials.hbm.xml
中使这些成为可能更改: <多对一名称=stuDetailsclass =pojo.StuDetails提取=selectcascade =all>
< column name =stu_details_idnot-null =true/>
< /多对一>
< column name =subject_idnot-null =true/>
< /多对一>
添加 cascade =all
属性为 stuDetails
和 subject
多对一标签。
- 级联属性是强制性的,当我们在对象之间应用关系
时,级联属性传输操作如果我们编写cascade =all
,那么在父类的变化对象也会对子类对象产生影响,如果我们写了cascade =all
,那么所有对父对象的插入,删除,更新操作都会影响到子对象。例如:如果我们对父
类对象应用插入(或更新或删除)操作,那么子类对象也将被存储到
数据库中。 / li>
I am trying to insert data to the DB using hibernate . Here is how I going perform that action
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();
But It gives me an error saying
Exception in thread "main" org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation
Here is my student details entity
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
My 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>
My Subject Entity looks like
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
Subject.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>
Here is the SubjetcHasStuDetails Entity
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>
Can someone help me on this error please ... Thanks..
In your SubjectHasStuDetials.hbm.xml
make these changes :
<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>
Add cascade="all"
attribute to both stuDetails
and subject
many-to-one tags.
- Cascade attribute is mandatory, when ever we apply relationship between objects, cascade attribute transfers operations done on one object onto its related child objects
- If we write
cascade = "all"
then changes at parent class object will be effected to child class object too, if we write cascade = "all" then all operations like insert, delete, update at parent object will be effected to child object also. - Example: if we apply insert(or update or delete) operation on parent class object, then child class objects will also be stored into the database.
这篇关于Hibernate TransientPropertyValueException保存数据时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!