Hibernate Introduction
Hibernate States
Transient – Not associated with any session. Newly created.
Persisted - Associated with the session. Have an
active mapping with a table row.
Detached – Not associated with session now. But, previously
persistent.
CRUD Methods
save()
- It returns an identifier.
- Entity is saved as soon as the save method is called. But, creates primary entity only. Mapped entities will be created at the time of committing or flushing. If we miss the committing or flushing in case the save method is called outside the transaction scope. Mapped elements will not be created.
- Calls update on persisted objects which are obtained by get() or load(). This update happens at the committing the transaction.
- Can be run outside the transaction scope.
- We can call save() on detached objects, but it results in creating a new entity
persist()
- It does not return anything
- Can be strictly called inside a transaction scope only
- Cannot call persist() on detached objects. Throws exception
saveOrUpdate()
- Based on the provided data, it can either insert/update the data. If the data is present in DB, update query is called. Else, save query is called.
- Detached objects can be made persistent by calling this method. Any Updates happen on this object after converted to persistent will be executed while committing the transaction.
- Can be run outside the transaction scope.
update()
- updates the existing entity. Updates are saved while committing the transaction.
- Detached Objects can be made persistent by calling this method.
merge()
public Object merge(Object object)
- Operation is similar to Update. Merge creates a copy of passed Object and returns it(clones).
- So any changes made to the returned object are saved while committing the transaction. Changes made to original object are ignored.
Comments
Post a Comment