我正在尝试使用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();
但这给我一个错误的说法
线程“主”中的异常org.hibernate.TransientPropertyValueException:非null属性引用了一个瞬态值- 必须在当前操作之前保存瞬态实例
这是我的学生详细信息实体
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
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>
这是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="all"
stuDetails
subject
cascade = “all”