There are in all four stages of Object in hibernate
When an object is instantiated it’s not persistent immediately let’s suppose we are creating simple object if I am having in Employee class.
We will create instance in this way,
Employee emp= new employee();
When we create object it is in transient state.
Once scope of the method where object is created finishes object will be garbage collected.
So in order to save such kind of objects Hibernate Persistence came in picture so the persistent manager allows us to save such kind of objects with the help of save method.
Persistent system manager will save the object having valid database entry and with valid primary key identifier.we create while declaring entity.
We Declare Primary key and entity using these ways.
<class name=”pojo1″ table=”pojo1″ discriminator-value=”s”>
<id name=”empid” type=”string” >
@javax.persistence.Id before the appropriate field in POJO with @Column
Class Employee implements Serializable
Private String userId
When I am going to save object with the help of hibernate session.You can save this object at this stage.Peristent instance are always associated with persistence context. If you want to save your changed records then you can set dynamic update=true that means once you changing some column values it will be updated automatically.
You can delete in entity instance by removing all the references on the object it will go to the remove state. Persistence manager signals that this object is in removes state and it is been scheduled for deletion at the end of the transaction so the remove object should not be used again for any course because it won’t be available as soon as the operation completes so you need to discard any kind of references you are holding for such kind of object.
Detached object is the one which is in initial stage is transient So now to save the transient object what we do we call persistent manage’s save or update method that will make an object is persistence and as long as we are in the persistence context that object is supposed to be in persistent stage.
Now imagine you’re closing this persistence context so once the work is done persistence context is closed but we are still having the handle to that reference so what we supposed to do with the reference now because we are done.The object whose reference you are holding now is no longer guaranteed to be synchronised with the database so it is no longer attached to persistence context.
You can work on the detached object can modify it you can do when you place in with it now the one thing you can do if you want to save the modification.
You can perform two operations
However Java persistence allows you or recommends you to do merging so merging implies The ability to take object from persistence context to the presentation layer and later reuse them in a new persistence context is advantage of JPA.
If you really like this tutorial please Share this tutorial.