Thursday 17 October 2013

Important Questions in Hibernate



Important Questions in Hibernate

In real time Hibernate is  called as DAO(Data Access Object).Because Hibernate Only Provide the Persistence Logic.DAO Pattern suggested that to separate the Business logic and Persistent logic.So in this process loose coupling between the business logic and persistent logic.And also reusable the persistent logic.

1.In Hibernate what happens when the same object is loaded for two times with  in a session?
ans:

  • A session of hibernate maintain a cache for storing the objects used in a session.With the help of the cache to reduce the no of round trips between the java application and database.
  • For the first time hibernate load the object from the database and it will be stored in a session cached .
  • Second time the object is loaded from the session cache but not from the database.

 We can understand  how many times the object is loaded from the database,using select command printed on the console.

2.what is the difference  between the save( ) method and persist( )method in hibernate?
ans:

  • save( ) method  return type is serializable  and persist( ) method return type is void.
  •  save ( ) method will save the object in session cache and returns the id of the object in  serialized format. 
  • persist( ) method will save the object in session cache and doesn't return any id of session object.
  • If the generator class is assigned the the programmer need to assign the id explicitly.In this case persist( ) method is used.
  • If the generator class is other than assign then hibernate will assign id of the object.In this case save( ) method is used.

3.What is the difference between save( ) and saveOrUpdate() methods?
ans:

  • save( ) method will perform only save operation.but saveOrUpdate( ) will perform save and update operations.
  • saveOrUpdate( ) method performs save operation if the id is new.If the id is persist then it perform update  operation.

4.In properties of pojo class,primitives or wrapper  is better type?
ans:

  • wrapper type is better than primitives .Because if we don't assign  a primitive  property value then it will save the property value is zero.It will miss understanding of the data.
  • To over come this problem we use wrapper type property  in this we don;t assign a primitive value then it will save the property value null is stored in database.So there will be no misunderstanding of the data.
5.why hibernate is recommended  to implement a pojo class java.io.Serializable interface?
ans:
  • If a database server is running on local machine then the object of pojo class may or may not be implement a serializable  interface
  • If a database server  is running on a remote machine in a network then only serializable objects are transfer in to the network.So we can must implement our pojo class object with serializable interface.
6.Can you create a hibernate application without creating a configuration file or not?
ans: Yes.we can create a hibernate application without configuration file.By using we can create a properties file or we can add a configuration pro-grammatically.

  • Before hibernate 3.x,Configuration file can be done either using properties file or pro grammatically .In hibernate 3.x we can use configuration xml file.
  • In properties file we can configured only connection properties and  hibernate properties.mapping resources can not be configured.
  • we can add the mapping resource to configuration explicitly by calling add Resource()method.
example:
hibernate.properties
     connection.driver_class=oracle.jdbc.OracleDriver
     connection url =jdbc:oracle:thin:@localhost:1521:XE
     connection username=system
     connection password=tiger
    dialect=org.hibernate.dialect.OracleDialect
    show_sql property tag
    
      In this above properties file we can configure only hibernate properties and connection properties.
      Next we can call the mapping resource through calling addResource() method in client application.
example:
         Configuration conf=new Configuration();
           conf.addResource("product.hbm.xml");
7 What is difference between the pool and cache?
ans: In hibernate mainly two difference are there in pool and cache.
  • pool  is a group of equal objects.we call them as stateless objects.But a cache is a group of unequal objects.we call them as state full objects.
  • In a pool a client has to wait until one of the object is free,when a pool is busy.where as in cache a client has to wait until any one of the object is free when cache is busy..
About Admin of the Blog:

Krishna babu is the founder of Alljobupdates .He is a Tech Geek,SEO Expert,Web Designer and a Pro Blogger. Contact Him Here

Follow him @ Facebook

0 comments:

Enjoyed? Share Your View With Us