小编典典

Hibernate TransientPropertyValueException保存数据时

hibernate

我正在尝试使用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>

有人可以帮我解决这个错误吗…谢谢..


阅读 404

收藏
2020-06-20

共1个答案

小编典典

在您 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" 属性不仅可以stuDetailssubject许多对一个标签。

  • 级联属性 是强制性的,只要我们在对象之间应用关系,级联属性就会将对一个对象执行的操作转移到其相关的子对象上
  • 如果我们编写,cascade = “all”则在父类对象上所做的更改也将影响子类对象,如果我们编写cascade =“ all”,则在父对象上进行的所有操作(如插入,删除,更新)也将对子对象产生影响。
  • 示例:如果我们对父类对象应用insert(或update或delete)操作,则子类对象也将存储到数据库中。
2020-06-20