Java Interview questions on Hibernate

Categories: EDUCATION

It's now time to review some questions derived from Hibernate, a well-known Java framework:

 

Q.1. When is it better to use plain SQL instead of ORM?

Ans. It's better to use plain SQL when:

(i) Complex queries need to be optimized for performance.

(ii) The database schema or query requirements are not well-supported by the ORM framework.

(iii) Direct control over SQL statements, database connections, or transactions is required.

 

Q.2. Difference between sorted and ordered collection?

Ans. In Java, a sorted collection maintains elements in a specific order defined by a comparator or by the natural ordering of elements, while an ordered collection maintains elements in the order they were inserted.

 

Q.3. How does second level cache work?

Ans. Second level cache in Hibernate stores objects in a shared cache region, typically across multiple sessions. When an entity is queried for the first time, it is fetched from the database and stored in the second level cache. Subsequent queries for the same entity can then be satisfied from the cache instead of hitting the database, improving performance.

 

Q.4. What is the difference between save() and persist() in Hibernate?

Ans. Both save() and persist() methods in Hibernate are used to save an entity to the database. However, save() returns the generated identifier immediately, while persist() doesn't guarantee immediate execution of the SQL INSERT statement; it may be executed later during flush time. Additionally, persist() is part of the JPA specification, while save() is specific to Hibernate.

 

Q.5. What is the difference between Hibernate and MyBatis?

Ans. (i) Hibernate is a full-fledged ORM framework that maps Java objects to database tables, manages database connections, and provides various querying mechanisms. MyBatis, on the other hand, is a lightweight persistence framework that uses SQL mapping files to map Java objects to SQL queries.

(ii) Hibernate is typically used for domain-driven development, where object-oriented modeling is prominent, while MyBatis is often preferred for projects where direct control over SQL queries is required, such as legacy database systems or complex SQL scenarios.

(iii) Hibernate provides caching mechanisms, automatic dirty checking, and transaction management, while MyBatis offers more control over SQL queries and mappings, allowing developers to write SQL queries directly.

Top articles
Who wrote the famous book 'The Road Ahead' Published at:- The Importance of Time Management during the SMLE Exam Published at:- Australian medical council exam Published at:- Australian Medical Council Exam For Foreign Medical Graduates Published at:- Teacher Interview Questions And Answers Published at:- Online earning websites for students without investment Published at:- Ministry Of Environment Forest And Climate Change Published at:- Jobs in Clinical Laboratory Science Published at:- Unveiling the Common Mistakes Students Make in Board Exam Preparation Published at:- Building Your Future: Uncovering the BEST Colleges for Engineering in America Published at:- Republic Day Fashion: Dressing with Pride in the Colors of the Indian Flag Published at:- Leveraging Operation Sesha AIM for Maximum Results Published at:- Impact of Globalization on Education In Trends and Challenges Published at:- Future of Education and Trends for PhD Candidates in 2024 Published at:- 8 Exciting Career Paths for Bachelor of Education Graduates in 2024 Published at:- Role of Artificial Intelligence in Stock Market Trading Published at:- Ultimate Guide to Passive Income through Real Estate Investing Published at:- Eco-Friendly Living: Tips for World Environment Day and Beyond Published at:- Heatwaves of How Man-Made Pollution is Fueling the Fire Published at:- Green Highway Signs: Navigating Safety and Visibility Published at:- Java Interview questions on Hibernate Published at:- Importance of STEM Education in Developing Countries Published at:- NEET Zoology MCQs Published at:- NEET BOTANY MCQs (Morphology of flowering plants) Published at:- Business Environment Multiple Choice Question MCQ Published at:- Government Policies for Business Policies Multiple Choice Question MCQ Published at:- Business Growth For Government Policies Multiple Choice Question MCQ Published at:- Sports (MCQs) Published at:- Books and Authors (MCQs) Published at:- Business Law (MCQs) Set-1 Published at:- Business Law (MCQs) Set-2 Published at:- Business Law (MCQs) Set-3 Published at:-