hibernate interview question

guys, when you take interview, have you ever answered below hibernate question ??

 

1.How to call stored procedure in mysql through hibernate

2.what is lazy initialisation in hibernate

 

 

 

 

1.answer:

As per sql-->

create a procedure in your sql by using the following code.
create or replace procedure first-procedure(x in number,y out number)
begin
y:=x*x;
end;
 write your sql connection code in hibernate configuration file. In your hibernate client application get the session obj. On the session object call the connection() method and get the connection. 
ex: Connection con=session.connection();
      CallableStatement cst=con.prepareCall("{call first-procedure(?,?)}");
      cst.registerOutParameter(2,Types.Integer);
      cst.setInt(1,20);
      cst.execute();
      int res=cst.getInt(2);
     s.o.p(res);
     ses.close();
2.answer:
Hibernate supports the feature of lazy initilasation for both entities and collections. What this actually means is, the Hibernate engine loads only those objects that we are querying for and doesnt try to fetch other entities(that are associated with the entity we are querying) or collections.
   
  An attribute 'lazy' can be used to let Hibernate know if the associated entity or collection has to be lazily loaded or prefetched.

Ex:
  

<set name="Children" lazy="false" inverse="true">

  <key column="FK_COL"/>

  <one-to-many class="Parent"/>

</set>

   This causes the collection to be eagerly fetched rather than doing a lazy fetch.  If on the other hand , the attribute value of lazy is set to true, then hibernate will not make an attempt to fire the query for fetching the collection object until the request is made by the user.

3.What is Hibernate proxy?
4.What is the difference between hibernate and spring JDBC template? List any advantages and disadvantages

 

 

3.answer

Proxies are created dynamically by subclassing your object at runtime. The subclass has all the methods of the parent, and when any of the methods are accessed, the proxy loads up the real object from the DB and calls the method for you. Very nice in simple cases with no object hierarchy. Typecasting and instanceof work perfectly on the proxy in this case since it is a direct subclass. 

 

4.answer:

Spring provides hibernate template and it has many advantages like
1) It removes boiler plate code like getting connection from data source, try/catch block for closing connection. So that developer can focus on writing business logic rather then writing boilier plate code every where.

2) Spring hibernateTemplate also throws RunTime exception compared to checkd exception which allows to remove writing try/catch block in each DAO.

3) It also gives richer template class, using which developer can write query code easily. This template class also allows to get session explicitly, so if developer wants to get session object and work on it, then it's possible.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值