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.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.