Hibernate / Hibernate interview questions II
Explain save, persist, saveOrUpdate and other object save methods in Hibernate.
Hibernate provides a handful of methods that store or update the object into the database. They are,
- and persist().
The purpose of these methods is as follows.
save method persists an entity. It assigns an identifier if entity doesn't exist in the database. If exists, save method performs an update. In both cases save method returns the generated ID of the entity.
Update method updates the existing object using identifier. If the identifier does not exist, it throws an exception.
saveOrUpdate method checks if the object is transient (i.e. it has no identifier) and if so it performs save that will make it persistent by generating an identifier and assigning it to session. If the object has an identifier already, it performs update().
saveOrUpdateCopy method is deprecated and no longer in use. Use merge method instead.
merge method is used to update the object to the database in any state of the session.
persist method makes a transient object persistent. However, it doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, thats why the method does not return generated ID.
Dogecoin! Earn free bitcoins up to $250 now by signing up.
Earn bitcoins upto $250 (free), invest in other Cryptocurrencies when you signup with blockfi. Use the referral link: Signup now and earn!
Using BlockFi, don't just buy crypto - start earning on it. Open an interest account with up to 8.6% APY, trade currencies, or borrow money without selling your assets.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
More Related questions...