Hibernate01

---From http://www.geekinterview.com/question_details/51897


1, What is the difference between and merge and update?


merge() adds the pojo class to the first level cache, firstly it checks in the database whether a given primary key value is available in the database or not. If it's available, it updates the value of the primary key. If it's not available, it inserts the data to the DB.

update() update the value for the given primary key.

merge is similar to saveOrUpdate();

2, what is the use of cascade in hbm file?


1)cascade="none", the default, tells the hibernate to ignore the association.
2)cascade="save-update" tells the hibernate to navigate the association when the transaction is committed, and when an object is passed to save() or update() and save newly instantiate transient instances and persist changes to detached instances.
3)cascade="delete" tells the hibernate to navigate the association when the transaction is delete the persist instance and when an object is passed to delete().
4)cascade="all" means the navigate both "save-update" and "delete" as well as calls to evict and lock.
5)cascade="delete-orphan" means delete any persistent entity instance has been removed(dereferenced) from the association(for example, from a collection). That's mean delete the father, then delete the sun.
5)cascade="all-delete-orphan" combine "all" and "delete-orphan".

3, what is the latest version of Hibernate using in current industry?


Hibernate 3.2

4, what is the main advantage of using the hibernate than using the sql?


The main advantage is it avoids writing a lot of queries, of course we need write some extent, but there is no need to use JDBC API. And it easily to migrate your code to different database without any query changes.

5, how to create primary key using hibernate?


1) in hbm.xml 
<id name="testId" type="String">
<column name="testColumn" length="40">
<generator class="increment">
</id>

2)use annotation
@Id
@Column(name="UserId")

6, How do we create new table apart from mapping with existing table ?


Hibernate automatically create new tables if there is corresponding existing POJOs .

If you know how to write/read from/to a existing table using hibernate, then you can easily create a table.
Step#1: create the POJO class for the table to be created.
Step#2: write the new hbm xml file for the table/entity, such as newTable.hbm.xml.
Step#3(optional): change the value of property hbm2ddl in hibernate.cfg.xml to "create", generally we use "update".
Step#4: Add the new configuration for the new table in hibernate.cfg.xml.
Step#5: Write a sample program to insert data to your new table---ensure the program can work.

If all the configuration stuff is good and execution runs like expected, check the database for the table and we should be able to find it with the sample records added.

7, what is lazy fetching in hibernate?


Lazy setting decides whether to load child objects while loading the parents object.  You need to do this setting respective hibernate mapping file of parent class.lazy=true(means not to load child object ). By default the lazy loading of the child object is true. This make sure that the child objects are not loaded unless they are explicitly invoked in the application by calling method getChild() on parent.  In this case, Hibernate issues a fresh database call to get the child object. 

In some cases, you do need to load child objects when parents is loaded, just make the lazy = false and hibernate will load the child objects when parent objects are loaded. 



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值