Hibernate TransientPropertyValueException When saving data

Steve Silva picture Steve Silva · Nov 23, 2015 · Viewed 19.7k times · Source

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

Answer

Madushan Perera picture Madushan Perera · Nov 23, 2015

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.