Hibernate interview questions

ust a summary from the article here: http://www.techfaq360.com/hibernate_interview_question


Difference between session.save() , session.saveOrUpdate() and session.persist()?

  • session.save()  Save does an insert and will fail if the primary key is already persistent.
  • session.saveOrUpdate() : saveOrUpdate does a select first to determine if it needs to do an insert or an update.
    session.persist() : Does the same like session.save().
    But session.save() return Serializable object(primary key), but session.persist() return void.


What are the general considerations or best practices for defining your Hibernate persistent classes?

  • You must have a default no-argument constructor for your persistent classes and there should be getXXX() (i.e accessor/getter) and setXXX( i.e. mutator/setter) methods for all your persistable instance variables.
  • You should implement the equals() and hashCode() methods based on your business key and it is important not to use the id field in your equals() and hashCode() definition if the id field is a surrogate key (i.e. Hibernate managed identifier). This is because the Hibernate only generates and sets the field when saving the object.

  • It is recommended to implement the Serializable interface. This is potentially useful if you want to migrate around a multi-processor cluster.
  • The persistent class should not be final because if it is final then lazy loading cannot be used by creating proxy objects. 

Difference between session.update() and session.lock() in Hibernate ?

  • Both of these methods and saveOrUpdate() method are intended for reattaching a detached object. 
  • The session.lock() method simply reattaches the object to the session without checking or updating the database on the assumption that the database in sync with the detached object.
    It is the best practice to use either session.update(..) or session.saveOrUpdate().
    Use session.lock() only if you are absolutely sure that the
    detached object is in sync with your detached object or if it does not matter because
    you will be overwriting all the columns that would have changed later on within the same transaction.

 

saveOrUpdate vs merge

1. If you retrieve an object and close session (the object is detached),

2. Then you open session and retrieve again( the second copy is managed by session)

3. try to save the first copy of the same object, hibernate will throw exception (not NonUniqueObjectExceptin)

4. Because 2 object is the same thing but one is manged, the other is not.

 

In this case, we can use merge to copy the value from object 1 -> object 2

 

http://www.stevideter.com/2008/12/07/saveorupdate-versus-merge-in-hibernate/


 

 

SaveOrUpdate() vs merge()

  • if the object is already persistent in this session, do nothing 

    if another object associated with the session has the same identifier, throw an exception 

    if the object has no identifier property, save() it 

    if the object's identifier has the value assigned to a newly instantiated object, save() it 

    if the object is versioned (by a <version> or <timestamp>), and the version property value is the same value assigned to a newly instantiated object, save() it, otherwise update() the object

  • if there is a persistent instance with the same identifier currently associated with the session, copy the state

    of the given object onto the persistent instance 

    if there is no persistent instance currently associated with the session, try to load it from the database, or

    create a new persistent instance the persistent instance is returned, the given instance does not become associated with the session, it remains detached

 Difference between list() and iterate() i9n Hibernate?

  • If instances are already be in the session or second-level cache iterate() will give better performance.
    If they are not already cached, iterate() will be slower
    than list() and might require many database hits for a simple query.

Difference between load() and get()

  • load() will throw an unrecoverable exception if there is no matching database row.
    get() will return null if there is no matching database row.

当object不是数据库中已有数据的对象的时候,不能使用session.refresh(object)因为refresh是从hibernate的session中去重新取object,如果session中没有这个对象,则会报错所以当你使用saveOrUpdate(object)之前还需要判断一下 (copy from here: http://chenying.blog.51cto.com/614874/134702 )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值