Prev Next

Hibernate / Hibernate interview questions

1. In a Parent-child relationship, how will you only access the columns from parent? 2. Different between session.get() and session.load() in Java. 3. @UniqueConstraint and @Column Unique attribute. 4. Difference between save and persist. 5. Difference between Hibernate createCriteria, createQuery, createSQLQuery. 6. What is HQL (Hibernate Query Language)? 7. Difference between @NotNull, @NotEmpty and @NotBlank. 8. What are the most common methods of Hibernate configuration? 9. What is ORM ? 10. What is hibernate? 11. What does ORM consists of ? 12. @Repository 13. @Embeddable 14. @Service 15. @Controller 16. Differentiate JPA and hibernate. 17. What are the ORM levels ? 18. Why do you need ORM tools like hibernate? 19. core interfaces of Hibernate. 20. Is SessionFactory a thread-safe object? 21. What role does the SessionFactory interface play in Hibernate? 22. Is Hibernate Session a thread-safe object? 23. What are the different states of object in hibernate? 24. How to make a immutable class in hibernate? 25. Different types of association in hibernate. 26. Is it possible to perform collection mapping with One-to-One and Many-to-One? 27. What is lazy loading in hibernate? 28. What is the difference between merge and update ? 29. How do you define sequence generated primary key in hibernate? 30. What are the Collection types in Hibernate ? 31. What is Hibernate proxy? 32. What are Callback interfaces? 33. What are the types of inheritance models in Hibernate? 34. Benefits of hibernate over JDBC? 35. Session Interface. 36. SessionFactory Interface. 37. Configuration Interface. 38. Transaction Interface. 39. Query and Criteria Interface. 40. What are Callback interfaces? 41. What is POJO? 42. Detached Criteria 43. Advantages of Detached criteria. 44. Explain N+1 SELECT problem in Hibernate. 45. List the strategies to eliminate the N+1 SELECT problem in Hibernate. 46. Explain high level architecture of Hibernate framework. 47. How do we see hibernate generated SQL on console? 48. Hibernate: Can a Entity Class be declared final? 49. How to call stored procedure in Hibernate? 50. Explain transaction management in hibernate. 51. Difference between fetch mode and fetch type in Hibernate. 52. What is the default fetch mode in Hibernate? 53. What is the difference between FetchType.LAZY and FetchType.EAGER in Java Persistence API? 54. Difference between using a @OneToMany and @ElementCollection annotation in Hibernate. 55. Explain the features introduced in Hibernate 5. 56. Which throws Exception when no row found: session.load vs session.get. 57. Difference between criteria and HQL In hibernate.

1. In a Parent-child relationship, how will you only access the columns from parent?

Lazy loading.

Read full answer

2. Different between session.get() and session.load() in Java.

load() get() Only use the load() method when the object exists in DB use get() if you are unsure about the existence of object. load() method throws an exception if the unique identifier could not be found. get() method returns null if the unique identifier not found in the database load() return...

Read full answer

3. @UniqueConstraint and @Column Unique attribute.

@UniqueConstraint and unique attribute of @Column instructs schema/DDL generation tool to generate the corresponding unique constraints however using that attributes on POJO doesn't implement constraints itself. @Entity @Table (uniqueConstraints = @UniqueConstraint (columnNames = { "email" , "emp...

Read full answer

4. Difference between save and persist.

Save() Returns generated Id after saving. The return type is serializable. Saves the value to DB immediately and keeps track of the entity until the end of the session(I have tried to change the entity values outside of the transaction, it does not show any effect when session commits) Does not s...

Read full answer

5. Difference between Hibernate createCriteria, createQuery, createSQLQuery.

There are 3 different ways we can create a SQL query in Hibernate. session.createQuery() session.createSQLQuery() session.createCriteria() Session.createQuery() creates Query object using the HQL syntax. For example: Query query = session.createQuery("from Student s where s.name like 'k%'"); Sess...

Read full answer

6. What is HQL (Hibernate Query Language)?

Hibernate Query Language is known as an object oriented query language. It is like structured query language (SQL). You don't need to learn SQL Database independent Simple to write query Hibernate offers a query language that embodies a very powerful and flexible mechanism to query, store, update...

Read full answer

7. Difference between @NotNull, @NotEmpty and @NotBlank.

@NotNull CharSequence, Collection, Map or Array object cannot be null, however can be empty. @NotEmpty The CharSequence, Collection, Map or Array object cannot be null and not empty (size > 0). @NotBlank The string is not null and the length is greater than zero. Here are the examples: String tes...

Read full answer

8. What are the most common methods of Hibernate configuration?

The most common methods of Hibernate configuration are: Programmatic configuration XML configuration (using hibernate.cfg.xml) Annotations

Read full answer

9. What is ORM ?

ORM stands for object/relational mapping. ORM is the automated persistence of objects in a Java application to the tables in a relational database.

Read full answer

10. What is hibernate?

Hibernate framework simplifies the development of java application to interact with the database. Hibernate is an open source, lightweight, ORM (Object Relational Mapping) tool.

Read full answer

11. What does ORM consists of ?

An ORM solution consists of the following four components: API for performing basic CRUD operations API to express queries refering to classes Facilities to specify metadata Optimization facilities : dirty checking,lazy associations fetching

Read full answer

12. @Repository

annotation that marks or clarifies the specific class as a Data Access Object.

Read full answer

13. @Embeddable

annotation specifies that the class will be used as a component. This class cannot have a primary key of its own, it uses the enclosing class primary key.

Read full answer

14. @Service

annotate service layer level classes.

Read full answer

15. @Controller

annotates presentation layer level classes.

Read full answer

16. Differentiate JPA and hibernate.

JPA is the specification for an ORM framework whereas hibernate is the implmentation.

Read full answer

17. What are the ORM levels ?

The ORM levels are, Pure relational (stored procedure.) Light objects mapping (JDBC) Medium object mapping Full object Mapping (composition,inheritance, polymorphism, persistence by reachability)

Read full answer

18. Why do you need ORM tools like hibernate?

Improved productivity High-level object-oriented API Less Java code to write No SQL to write Improved performance Sophisticated caching Lazy loading Eager loading Improved maintainability A lot less code to write Improved portability ORM framework generates database-specific SQL for you

Read full answer

19. core interfaces of Hibernate.

The core interfaces of Hibernate framework are: Configuration SessionFactory Session Query Criteria Transaction

Read full answer

20. Is SessionFactory a thread-safe object?

Yes, SessionFactory is a thread-safe object, many threads can access it simultaneously.

Read full answer

21. What role does the SessionFactory interface play in Hibernate?

The application obtains Session instances from a SessionFactory. There is typically a single SessionFactory for the whole application created during application initialization. The SessionFactory caches generate SQL statements and other mapping metadata that Hibernate uses at runtime. It also hol...

Read full answer

22. Is Hibernate Session a thread-safe object?

No, Session is not a thread-safe object, many threads can't access it simultaneously. In other words, you cannot share it between threads.

Read full answer

23. What are the different states of object in hibernate?

There are 3 states of object (instance) in hibernate. Transient : The object is in transient state if it is just created but has no primary key (identifier) and not associated with session. Persistent : The object is in persistent state if session is open, and you just saved the instance in the d...

Read full answer

24. How to make a immutable class in hibernate?

If you mark a class as mutable="false", class will be treated as an immutable class. By default, it is mutable="true".

Read full answer

25. Different types of association in hibernate.

There are four types of association mapping in hibernate. One to One One to Many Many to One Many to Many

Read full answer

26. Is it possible to perform collection mapping with One-to-One and Many-to-One?

No, collection mapping can only be performed with One-to-Many and Many-to-Many.

Read full answer

27. What is lazy loading in hibernate?

Lazy loading in hibernate improves the performance. It loads the child objects on demand. Since Hibernate 3, lazy loading is enabled by default, you don't need to do lazy="true". It means not to load the child objects when parent is loaded.

Read full answer

28. What is the difference between merge and update ?

Use update() if you are sure that the session does not contain an already persistent instance with the same identifier, and merge() if you want to merge your modifications at any time without consideration of the state of the session.

Read full answer

29. How do you define sequence generated primary key in hibernate?

Using tag. SEQUENCE_NAME

Read full answer

30. What are the Collection types in Hibernate ?

Bag Set List Array Map

Read full answer

31. What is Hibernate proxy?

The proxy attribute enables lazy initialization of persistent instances of the class. Hibernate will initially return CGLIB proxies which implement the named interface. The actual persistent object will be loaded when a method of the proxy is invoked.

Read full answer

32. What are Callback interfaces?

Callback interfaces allow the application to receive a notification when something interesting happens to an object, for example, when an object is loaded, saved, or deleted. Hibernate applications don't need to implement these callbacks, but they're useful for implementing certain kinds of gener...

Read full answer

33. What are the types of inheritance models in Hibernate?

There are three types of inheritance models in Hibernate: Table per class hierarchy Table per subclass Table per concrete class

Read full answer

34. Benefits of hibernate over JDBC?

Hibernate is database independent while in case of JDBC, developer has to write database specific queries. In case of Hibernate developer doesn't need to be an expert of writing complex queries as HQL simplifies query writing process while in case of JDBC, its job of developer to write and tune q...

Read full answer

35. Session Interface.

This is the primary interface used by hibernate applications. The instances of this interface are lightweight and are inexpensive to create and destroy. Hibernate sessions are not thread safe.

Read full answer

36. SessionFactory Interface.

This is a factory that delivers the session objects to hibernate application. Generally there will be a single SessionFactory for the whole application and it will be shared among all the application threads.

Read full answer

37. Configuration Interface.

This interface is used to configure and bootstrap hibernate. The instance of this interface is used by the application in order to specify the location of hibernate specific mapping documents.

Read full answer

38. Transaction Interface.

This is an optional interface but the above three interfaces are mandatory in each and every application. This interface abstracts the code from any kind of transaction implementations such as JDBC transaction, JTA transaction.

Read full answer

39. Query and Criteria Interface.

This interface allows the user to perform queries and also control the flow of the query execution.

Read full answer

40. What are Callback interfaces?

These interfaces are used in the application to receive a notification when some object events occur. Like when an object is loaded, saved or deleted. There is no need to implement callbacks in hibernate applications, but they're useful for implementing certain kinds of generic functionality.

Read full answer

41. What is POJO?

POJO stands for plain old java objects. These are just basic JavaBeans that have defined setter and getter methods for all the properties that are there in that bean. Besides they can also have some business logic related to that property.

Read full answer

42. Detached Criteria

The detached criteria allows you to create the query without Session. Then you can execute the search in an arbitrary session. Using a DetachedCriteria is exactly the same as a Criteria except you can do the initial creation and setup of your query without having access to the session. When it co...

Read full answer

43. Advantages of Detached criteria.

useful to make join conditions, subselects, and to query outside the current session. code reuse

Read full answer

44. Explain N+1 SELECT problem in Hibernate.

Hibernate ends up executing N+1 SQL queries to populate a collection of N elements referred as N+1 SELECT problem that occurs due to N+1 lazy loading and load on demand fetching strategy. Let's say you have a collection of company objects (database rows), and each company has a collection of empl...

Read full answer

45. List the strategies to eliminate the N+1 SELECT problem in Hibernate.

Here are some of the strategies to solve the N+1 problem. pre-fetching in batches, this will reduce N+1 problem to N/K + 1 problem where K is size of batch. subselect fetching strategy. and disable lazy loading.

Read full answer

46. Explain high level architecture of Hibernate framework.

The important components in Hibernate architecture are, SessionFactory, Session, Persistent objects, and Transaction. org.hibernate. SessionFactory : Refers to the cache of compiled mappings (hbm?s or annotation based) for a single database. org.hibernate. Session : Represents a conversation betw...

Read full answer

47. How do we see hibernate generated SQL on console?

Setting the property show_sql to true at the hibernate configuration file enable viewing SQL on the console for debugging purposes. true

Read full answer

48. Hibernate: Can a Entity Class be declared final?

Yes , a Hibernate Entity class can be declared final, however it is not a good practice. Hibernate uses the proxy pattern for performance improvement during lazy association, by making an entity final, Hibernate will no longer be able to use a proxy as Java doesn't allow the final class to be ext...

Read full answer

49. How to call stored procedure in Hibernate?

In Hibernate, there are 3 approaches to call a database store procedure. Use createSQLQuery() to call a store procedure directly. Use @NamedNativeQueries annotation. Declare your store procedure inside the sql-query tag.

Read full answer

50. Explain transaction management in hibernate.

In hibernate framework, we have Transaction interface that defines the unit of work. It maintains abstraction from the transaction implementation (JTA,JDBC). A transaction is associated with Session and instantiated by calling session.beginTransaction (). The transaction interfaces defines the be...

Read full answer

51. Difference between fetch mode and fetch type in Hibernate.

FetchType (Lazy/Eager) instructs whether the entity to be loaded eagerly or lazy, when there is a call. FetchMode (Select/Join) instructs whether the entitity to be loaded with additional select or in one query with join or subselect.

Read full answer

52. What is the default fetch mode in Hibernate?

If the Hibernate annotation @Fetch is not present on a field, then the default FetchMode for this field is decided based on the FetchType. If the field has FetchType = EAGER, then FetchMode = JOIN. Otherwise, FetchMode = SELECT.

Read full answer

53. What is the difference between FetchType.LAZY and FetchType.EAGER in Java Persistence API?

The EAGER strategy is a requirement on the persistence provider runtime that data must be eagerly fetched. The LAZY strategy is a hint to the persistence provider runtime that data should be fetched lazily when it is first accessed.

Read full answer

54. Difference between using a @OneToMany and @ElementCollection annotation in Hibernate.

@ElementCollection maps non-entities (embeddable or basic) while @OneToMany is used to map entities.

Read full answer

55. Explain the features introduced in Hibernate 5.

Support of Java 8 in Hibernate 5 is one of the major changes and it supports Java 8 Date and Time API. Hibernate 5 supports full-text search through Lucene 5. Hibernate 5 through OGM provides the support for persistence to the NoSQL Databases such as MongoDB, REDIS. @NamedQuery and other Hibernat...

Read full answer

56. Which throws Exception when no row found: session.load vs session.get.

session.load() will throw an ObjectNotFoundException when no row found while session.get returns null.

Read full answer

57. Difference between criteria and HQL In hibernate.

Criteria. HQL query Criteria query performs only SELECT operations. HQL query performs both SELECT and NON-SELECT operations. HQL can be used to perform SELECT, INSERT, UPDATE, DELETE. Criteria supports pagination. HQL doesn't support pagination. Criteria query is safe from SQL injection because ...

Read full answer

«
»

Comments & Discussions