java Hibernate TransientPropertyValueException 保存数据时

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33872396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:13:12  来源:igfitidea点击:

Hibernate TransientPropertyValueException When saving data

javahibernatehbmxml

提问by Steve Silva

I am trying to insert data to the DB using hibernate . Here is how I going perform that action

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

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

线程“main”org.hibernate.TransientPropertyValueException 中的异常:非空属性引用了一个瞬态值 - 在当前操作之前必须保存瞬态实例

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

我的学生详情 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

主题.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

这是 SubjetcHasStuDetails 实体

 public class SubjectHasStuDetails  implements java.io.Serializable {


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

SubjectHasStuDetials.hbm.xml

SubjectHasStuDeticals.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..

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

回答by Madushan Perera

In yourSubjectHasStuDetials.hbm.xmlmake these changes :

在您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>

Add cascade="all"attribute to both stuDetailsand subjectmany-to-one tags.

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

  • Cascade attributeis 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.
  • 级联属性是强制性的,当我们应用对象之间的关系时,级联属性会将在一个对象上完成的操作转移到其相关的子对象上
  • 如果我们写cascade = “all”那么父类对象的变化也会影响到子类对象,如果我们写cascade =“all”那么所有在父对象上的插入、删除、更新等操作也会影响到子对象。
  • 示例:如果我们对父类对象应用插入(或更新或删除)操作,那么子类对象也将存储到数据库中。