1
votes

enter image description here

                                     EER Diagram

*Please note that course entity is not related to this question

Here I use save() method to save an object of Instructor to the database.[Instructor and InstructorDetails have a relationship of cascading allowed one to one bidirectional ,so saving the instructor object will save the instructorDetails object too] .In that case I got an error of object references an unsaved transient instance - save the transient instance before flushing.But when I use the persists() method that doesn't came.So but when I change the cascading type to CascadeType.ALL It was okay with the save() method too.But you can see I just removed the cascade type of REMOVE only in the following code ,so how it's going to be affected to the method like save() ?

Instructor Model class

public class Instructor{

@OneToOne(cascade={CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH})
//Here you can see I just only neglected the cascade type of REMOVE only 
@JoinColumn(name="instructor_detail_id")
private InstructorDetails instructorDetail;
//
//

}

InstructorDetails Model class

public class InstructorDetails{

 @OneToOne(mappedBy ="instructorDetail",cascade = 
{CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH} )
//Here you can see I just only neglected the cascade type of REMOVE only 
private Instructor instructor;
//
//

}

1

1 Answers

1
votes

persist() makes a transient instance persistent. However, it does not guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time. persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries.

save() does guarantee to return an identifier. If an INSERT has to be executed to get the identifier ( e.g. "identity" generator, not "sequence"), this INSERT happens immediately, no matter if you are inside or outside of a transaction.

You can fine more information on Hibernate Documentation page, point 11.2. Making objects persistent