YouTube Icon

Interview Questions.

Hibernate Interview Questions and Answers - Jul 16, 2022

fluid

Hibernate Interview Questions and Answers

Q1. What is Hibernate?

Ans: Hibernate is an ORM (Object-relational Mapping) framework, which lets in the developer to concentrate on business logic by means of looking after endurance of information by itself. Java developer can write code using object and Hibernate can contend with growing the ones item from information loaded from the database and saving replace again to the database.

Q2. What is ORM?

Ans: ORM (Object Relational Mapping) is the essential idea of Hibernate framework which maps database tables with Java Objects after which gives diverse API’s to perform one of a kind sorts of operations at the records tables.

Q3. What’s the usage of Configuration Interface in hibernate?

Ans: Configuration interface of hibernate framework is used to configure hibernate. It’s extensively utilized to bootstrap hibernate. Mapping documents of hibernate are placed using this interface.

Q4. What are the benefits of the usage of Hibernate?

Ans: Below are the benefits of the use of Hibernate.

Hibernate looks after mapping Java instructions to database tables the usage of XML documents and with out writing any line of code.

Provides easy APIs for storing and retrieving Java items at once to and from the database.

If there is exchange in Database or in any table then the most effective need to exchange XML file homes.

Abstract away the strange SQL types and provide us to work around familiar Java Objects.

Hibernate does not require an software server to function.

Manipulates Complex associations of gadgets of your database.

Minimize database get right of entry to with smart fetching techniques.

Provides Simple querying of statistics.

Q5. What are POJOs and what’s their importance?

Ans: POJOs( Plain Old Java Objects) are java beans with proper getter and setter techniques for each and every homes.

Use of POJOs rather than easy java training effects in an green and nicely constructed code.

Q6. What are the benefits of Hibernate over JDBC? 

Ans: Apart from Persistence i.E. Saving and loading facts from Database, Hibernate also gives following advantages

Caching

Lazy Loading

Relationship control and presents code for mapping an object to the records

The developer is loose from writing code to load/save information into the database

Q7. How can we create session manufacturing facility in hibernate?

Ans: To create a consultation manufacturing facility in hibernate, an item of configuration is created first which refers back to the course of configuration report and then for that configuration, consultation manufacturing facility is created as given in the example beneath:

Java:

Configuration config = new Configuration();

config.AddResource("myinstance/configuration.Hbm.Xml");

config.SetProperties( System.GetProperties() );

SessionFactory sessions = config.BuildSessionFactory();
 

Q8. What is N+1 SELECT hassle in Hibernate? 

Ans: The N+1 SELECT trouble is a end result of lazy loading and cargo on call for fetching method. In this case, Hibernate ends up executing N+1 SQL queries to populate a collection of N elements. For instance, if you have a List of N Items where each Item has a dependency on a collection of Bid object. Now if you want to discover the best bid for each item then Hibernate will fire 1 question to load all objects and N subsequent queries to load Bid for every object. So as a way to discover the very best bid for each item your utility end up firing N+1 queries.

Q9. What is the requirement for a Java object to come to be Hibernate entity object? 

Ans: It must now not be very last and have to provide a default, no-argument constructor. See the targeted answer to examine extra about the special requirement for a Java item to become Hibernate Entity.

Q10. What the three inheritance models are of hibernate?

Ans: Hibernate has following 3 inheritance models:

Tables Per Concrete Class

Table in keeping with elegance hierarchy

Table in line with sub-magnificence

Q11. Mention the benefits of the use of Hibernate template?

Ans: Following are a few key advantages of using Hibernate template:

Session remaining is automatic.

Interaction with hibernate session is simplified.

Exception managing is computerized.

Q12. List out the four ORM stages  in hibernate?

Ans: Following are the 4 ORM tiers in hibernate:

Pure Relational

Light Object Mapping

Medium Object Mapping

Full Object Mapping

Q13.  What are one of a kind styles of caches available in Hibernate? 

Ans: This is every other common Hibernate interview question. Hibernate affords the out-of-box caching answer but there are numerous caches e.G. First stage cache, 2d level cache and question cache. First degree cache is maintained at Session level and can not be disabled however the 2d degree cache is needed to be configured with external cache issuer like EhCache.

Q14. What is criterion question in hibernate? 

Ans: Criteria is a simplified API for retrieving entities by composing Criterion objects also referred to as Criterion question. This is a totally convenient method for capability like "search" monitors where you may filter statistics on a couple of conditions as proven within the following instance:

List books = session.CreateCriteria(Book.Magnificence) .Add(Restrictions.Like("call", "java%") ) .Add(Restrictions.Like("published_year", "2015")) .AddOrder(Order.Asc("name") ) .Listing();

This may be a hard query in case you are not the use of Hibernate on a each day basis, I even have interviewed numerous Java builders who have used Hibernate however does not recognize approximately Criterion query or API.

Q15. What are other ORM frameworks? Any alternative of Hibernate?

Ans: This is a trendy question, every now and then requested to start the communication and different times to finish the interview. EJB and TopLink from Oracle are two of the most popular opportunity to Hibernate framework.

Q16. What is the distinction between store() and saveOrUpdate() approach of Hibernate? 

Ans: Though both save() and saveOrUpdate() approach is used to keep object into Database, the important thing distinction between them is that shop can most effective INSERT statistics but saveOrUpdate() can both INSERT or UPDATE statistics.

Q17. What is distinction among getCurrentSession() and openSession() in Hibernate? 

Ans: An interesting Hibernate interview question as you might have used each getCurrentSession() and openSession() to attain an instance of Session object. I have left this question unanswered which will solution or find an answer based totally in your revel in.

Q18. What is the difference between first and second level cache in Hibernate? 

Ans: This is again comply with-up of preceding Hibernate interview query. The first level cache is maintained at Session level whilst the second one stage cache is maintained at SessionFactory level and shared by way of all periods.

Q19. Does Hibernate Session interface is thread-safe in Java? 

Ans: No, Session item is not thread-safe in Hibernate and supposed to be used with-in single thread in the software.

Q20. Does SessionFactory is thread-secure in Hibernate? 

Ans: SessionFactory is both Immutable and thread-safe and it has simply one single example in Hibernate utility. It is used to create Session object and it also offer caching through storing SQL queries stored through multiple session. The second stage cache is maintained at SessionFactory level. This can be a difficult and complex query for much less experienced Java developers who are not acquainted with thread-protection and Immutability.

Q21. What is different between Session and Sessionfactory in Hibernate?

Ans: This is every other famous Hibernate interview question, on the whole at a telephonic round of interviews. The principal difference among Session and SessionFactory is that former is a unmarried-threaded, brief-lived object even as later is Immutable and shared by means of all Session. It also lives till the Hibernate is jogging. Another distinction among Session and SessionFactory is that former provides first level cache whilst SessionFactory presents the Second level cache.

Q22. What is Hibernate Query Language (HQL)? 

Ans: Hibernate question language, HQL is an item-oriented extension to SQL. It lets in you to question, save, replace, and retrieve objects from a database without the use of SQL. This query is likewise just like the sooner question approximately Criterion question, Java developers who have now not used Hibernate notably will now not know an awful lot about functions like HQL and Criterion.

Q23. When do you operate merge() and update() in Hibernate? 

Ans: This is one of the complicated Hibernate interview questions. You need to use replace() in case you are sure that the Hibernate consultation does no longer contain an already continual instance with the same identity and use merge() if you want to merge your adjustments at any time without considering the state of the session.

Q24. The distinction between sorted and ordered collection in Hibernate? 

Ans: The predominant distinction between sorted and ordered series is that looked after collection type the records in JVM's heap memory the usage of Java's collection framework sorting techniques while ordered collection is looked after the usage of order through clause inside the database itself. A looked after collection is more desirable for small dataset however for a large dataset, it is higher to use ordered series to avoid OutOfMemoryError in Java utility.

Q25. How do you log SQL queries issued by using the Hibernate framework in Java software?

Ans: You can use the show_sql property to log SQL queries issued by means of the Hibernate framework, Just upload the subsequent line on your Hibernate configuration file:

<property name=”show_sql”> real </property>

Q26. What are the three states of a Hibernate Persistence item can be? 

Ans: The Hibernate chronic or entity item can live in following three states:

temporary

 continual

 detached

Q27. In what number of ways, gadgets may be fetched from database in hibernate?

Ans: Hibernate provides following 4 approaches to fetch objects from database:

Using HQL

Using identifier

Using Criteria API

Using Standard SQL

Q28. What is ORM metadata?

Ans: All the mapping between lessons and tables, homes and columns, Java kinds and SQL types and many others is defined in ORM metadata.

Q29. What’s the role of JMX in hibernate?

Ans: Java Applications and additives are managed in hibernate by way of a general API known as JMX API. JMX provides equipment for development of efficient and sturdy dispensed, internet based solutions.

Q30. What is the difference among the brief, chronic and indifferent kingdom in Hibernate? 

Ans: New gadgets created in Java application however now not related to any hibernate Session are stated to be within the brief kingdom. On the other hand, an item which is related to a Hibernate session is called Persistent item. While an object which changed into in advance related to Hibernate session but currently it's not associate is known as a detached item. You can call shop() or persist() method to save those object into the database and produce them into the Persistent country. Similarly, you may re-attach a detached object to hibernate periods by calling both replace() or saveOrUpdate() approach.

Q31. Which cache is utilized by Session Object in Hibernate? First level or second level cache? 

Ans: A Session item uses the primary-degree cache. As I informed earlier than the second level cache is used at SessionFactory level. This is a good query to check if Candidate has been running in hibernate or no longer. If he has now not labored in Hibernate from a long time then he might get burdened in this query.

Q32. What one-of-a-kind fetching techniques are of hibernate?

Ans: Following fetching strategies are available in hibernate:

Join Fetching

Batch Fetching

Select Fetching

Sub-choose Fetching

Q33. What’s using model belongings in hibernate?

Ans: Version assets is used in hibernate to recognize whether or not an item is in transient kingdom or in indifferent state.

Q34. What is characteristic oriented programming?

Ans: In Attribute orientated programming, a developer can add Meta facts (attributes) within the java source code to feature extra significance within the code. For Java (hibernate), characteristic oriented programming is enabled via an engine called XDoclet.

Q35. Does hibernate aid polymorphism?

Ans: Yes, hibernate fully helps polymorphism. Polymorphism queries and polymorphism institutions are supported in all mapping techniques of hibernate.

Q36. How are we able to map the classes as immutable?

Ans: If we don’t want an utility to update or delete gadgets of a category in hibernate, we can make the magnificence as immutable by means of putting mutable=false

Q37. What’s widespread hibernate glide the use of RDBMS?

Ans: General hibernate go with the flow concerning RDBMS is as follows:

Load configuration report and create item of configuration class.

Using configuration object, create sessionFactory item.

From sessionFactory, get one consultation.

Create HQL question.

Execute HQL query and get the consequences. Results could be in the shape of a list.

Q38. What’s the usage of consultation.Lock() in hibernate?

Ans: session.Lock() method of consultation elegance is used to reattach an item which has been detached in advance. This technique of reattaching doesn’t take a look at for any information synchronization in database while reattaching the item and as a result may additionally lead to lack of synchronization in facts.




CFG