Showing posts with label Hibernate Interview. Show all posts
Showing posts with label Hibernate Interview. Show all posts

Monday, 14 October 2013

Difference between load and get method of the Hibernate


Difference between load and get method of the  Hibernate





Get
Load
Hibernate Session class returns null if object is not found in cache as 
well as on database
load() method throws ObjectNotFoundException if object is not found on cache as well as on database but never return null.
Get method never returns a proxy, it

 either returns null or fully initialized 

Object
load() method may return proxy, which is the object with ID but without initializing other properties, which is lazily initialized.
Get method will result in slightly lower performance,
load method will result in slightly better performance,
Get method always hit database
load() method may not always hit the
database

Wednesday, 9 October 2013

Difference between save and persist method in Hibernate

Difference between save and persist method in Hibernate

  1. First difference between save and persist is there return type. Similar to save method persist also INSERT records into database but return type of persist is void while return type of save is Serializable object.
  2. Another difference between persist and save is that both methods make a transient instance persistent. However, persist() method doesn't guarantee that the identifier value will be assigned to the persistent instance immediately, the assignment might happen at flush time.
  3. One more thing which differentiate persist and save method in Hibernate is that is their behavior on outside of transaction boundaries. persist() method guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. save() method does not guarantee the same, it returns an identifier, and if an INSERT has to be executed to get the identifier (e.g. "identity" generator), this INSERT happens immediately, no matter if you are inside or outside of a transaction.

Hibernate Output-01


What is the Output of this program 




public static void main(String[] args) {
              UserDetails user= new UserDetails();
       user.setUserName("Fist User");
                  SessionFactory sessionFactory =     new Configuration().configure().buildSessionFactory();
           Session session = sessionFactory.openSession();
           session.beginTransaction();
user.setUserName("Second User");
user.setUserName("Third  User");
           session.save(user);
   user.setUserName("Fourth   User");  
   user.setUserName("Fifth   User");  
           session.getTransaction().commit();
           session.close();
                      
   }
   
What is the Current data stored in the data base  ?

Ans is - Fifth User 
 Please go through diagram

 

Tuesday, 8 October 2013

What is Hibernate Caching


What is Hibernate Caching?
Caching functionality is designed to reduce the amount of necessary database access. When the objects that are cached reside in memory. You have the flexibility to limit the usage of memory and store the items in disk storage. The implementation will depend on the underlying cache manager.



  1. FirstLevel Cache
    1. First-level cache always Associates with the Session object. Hibernate uses this cache by default. Here, it processes one transaction after another one, means won't process one transaction many times. Mainly it reduces the number of SQL queries it needs to generate within a given transaction. That is instead of updating after every modification done in the transaction, it updates the transaction only at the end of the transaction.
  2. Second Level Cache
    1. Second-level cache always associates with the Session Factory object. While running the transactions, in between it loads the objects at the Session Factory level, so that those objects will available to the entire application, don’t bounds to single user. Since the objects are already loaded in the cache, whenever an object is returned by the query, at that time no need to go for a database transaction. In this way the second level cache works.
  3. Query Cache
    1. Query Cache is used to cache the results of a query. When the query cache is turned on, the results of the query are stored against the combination query and parameters. Every time the query is fired the cache manager  checks for the combination of parameters and query. If the results are found in the cache, they are returned, otherwise a database transaction is initiated.  As you can see, it is not a good idea to cache a query if it has a number of parameters, because then a single parameter can take a number of values. For each of these combinations the results are stored in the memory. This  can lead to extensive memory usage.

Who and when developed Hibernate ?

Who and when developed Hibernate  ?

Ans -2001,Galvin king

Very Impotent Links for Java and Portal development development

Jar Download for Spring  Maven Or Gradel Dependency for Spring