object references an unsaved transient instance

I’ve faced with this error many times when trying to save an object to the db using hibernate.
The main reason is caused by the default behavior of hibernate to the relations between objects.
If you do not specify the cascade type in the hbm.xml file, then hibernate acts like it is a null relation which means
the related object is already in the db. For example, assume there are two concrete classes called Person and Address; Person has a one to many one directional
association with the Address..

public class Person {
private String name;
private String surname;
private Address address;



}

public class Address {
private String street;
private int zipcode;
private String city;



}

The association in the Person.hbm.xml will look like;

one-to-many name=”address” class=”Address”

Everything seems fine however when we try to save a person instance the error following exception will likely to occur.

//Assume you have the ready person instance at this point
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
session.save(Person);
tx.commit();
session.close();

object references an unsaved transient instance – save
the transient instance before flushing

The transient instance the error mentioned is the address of the person because hibernate thinks that the address is already in the db since you are not referring to a persistent address object.

In order to overcome this error, you can either define a cascade for the relation like “save-update, all etc”. or save the address before saving the person.

1) one-to-many name=”address” class=”Address” cascade=”all-delete-orphan”
After changing to this, the same code will work.

2) If you do not want to change the xml, you can try this;
//Assume you have the ready person instance at this point
Session session = sessions.openSession();
Transaction tx = session.beginTransaction();
session.save(person.getAddress());
session.save(person);
tx.commit();
session.close();

8 Responses to object references an unsaved transient instance

  1. jgeiger says:

    Funny. I had this same issue earlier this week…

    I went with solution #2…

  2. Ángel says:

    I had this issue too but in my case I had a many-to-one relationship with an attibute not-null=false. My problem was that I didn’t initialize this attribute to null when I declare it at my persistent object.

    Regards

  3. joe says:

    cascade=”all|none|save-update|delete”

    or

    cascade=”all-delete-orphan”

    ??

  4. srini says:

    i am also gettin same error

  5. Daniel says:

    Thanks. This helped me fix it!

  6. Arun MR says:

    Thanks for the post. It helped me fix it.

    If the mapping involve a collection cascade is at the Set like

    set name=”taskEntryList” table=”TimesheetTaskEntry” cascade=”save-update”
    key column=”daily_entry_id”
    one-to-many class=”com.uclock.timesheet.TimeSheetTaskEntry”
    set

    Probably it is obvious. I’m new to this.🙂

  7. Diego says:

    I’m working in a scenario similar to the one described, but using hibernate as JPA onder Spring.
    The “address” column in my DB can be null.
    The exception is thrown evrytime the address is not set, but how can I tell hibernate not
    to save null “unsaved transient instances”?

    I tryed with the annotaion @JoinColumn(name = “address”, nullable=”true”) but it doesn’t work

    Any hint?

%d bloggers like this: