75 Hibernate Interview Questions And Answers For Experienced. Here in this post coding compiler presenting a list of 75 advanced hibernate interview questions for freshers, 1 year experience, 2 years experience, 3 year experience, 4 year experience, 5 year experience.
Hibernate Interview Questions
- Hibernate Interview Questions For Freshers
- Hibernate Interview Questions For 1 Year Experience
- Hibernate Interview Questions For 2 Years Experience
- Hibernate Interview Questions For 3 Years Experience
- Hibernate Interview Questions For 4 Years Experience
- Hibernate Interview Questions For 5 Years Experience
Hibernate Interview Questions For Freshers
Question # 1 What is hibernate in java?
Answer # Hibernate ORM (Hibernate in short) is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database. Hibernate also provides data query and retrieval facilities.
Question # 2 Is hibernate better than JDBC?
Answer # JDBC will always give better performance as compared to Hibernate for most of the database vendors. … The choice of hibernate over jdbc and sql queries is not because of the performance but because of reasons mainly object persistence and database independence in terms of not writing database specific queries.
Question # 3 Why do we need hibernate in Java?
Answer # So with JDBC, mapping between Java objects and database tables is done manually. Hibernate reduces lines of code by maintaining object-table mapping itself and returns result to application in form of Java objects. Hibernate, with Transparent Persistence, cache is set to application work space.
Question # 4 What is the use of ORM in Java?
Answer # ORM allows you to use java objects as representation of a relational database. It maps the two concepts (object-oriented and relational) Hibernate is an ORM framework – you describe how your objects are represented in your database, and hibernate handles the conversion.
Question # 5 What is the difference between JPA and Hibernate?
Answer # JPA is the interface, Hibernate is one implementation of that interface. JPA is a specification for accessing, persisting and managing the data between Java objects and the relational database. As the definition says its API, it is only the specification. Hibernate is a JPA provider.
Hibernate Interview Questions And Answers
Question # 6 What is the use of Session in hibernate?
Answer # The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service. The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)
Question # 7 What is the architecture of hibernate?
Answer # Hibernate makes use of the database and configuration data to provide persistence services (and persistent objects) to the application.
Question # 8 What is ORM hibernate?
Answer # Hibernate ORM (Hibernate in short) is an object-relational mapping tool for the Java programming language. It provides a framework for mapping an object-oriented domain model to a relational database.
Question # 9 Can we use only JPA without hibernate?
Answer # You have to specify the persistence provider(Hibernate,EclipseLink) in order to use the JPA implementation. The persistence providers have the implementaion classes for JPA specifications. You can’t just use JPA, cause it is an API =), but there are plenty JPA implementations: EclipseLink.
Question # 10 What are the fetching strategies supported by hibernate?
Answer # Fetch strategies can be declared in the O/R mapping metadata, or over-ridden by a particular HQL or Criteria query. Hibernate defines the following fetching strategies:
- Join fetching
- Select fetching
- Subselect fetching
- Batch fetching
- Immediate fetching
- Lazy collection fetching
- “Extra-lazy” collection fetching
- Proxy fetching
- “No-proxy” fetching
- Lazy attribute fetching
Hibernate Interview Questions For Experienced
Question # 11 What is a polymorphic association?
Answer # Polymorphic association is a term used in discussions of Object-Relational Mapping with respect to the problem of representing in the relational database domain, a relationship from one class to multiple classes. In statically typed languages such as Java these multiple classes are subclasses of the same superclass.
Question # 12 What is the difference between session and Sessionfactory in hibernate?
Answer # SessionFactory is Hibernate’s concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. Sessions are opened by a SessionFactory and then are closed when all work is complete.
Question # 13 What is the difference between load and get method in hibernate?
Answer # Session.load(): It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. If no row found , it will throws an ObjectNotFoundException.
Question # 14 Is hibernate Sessionfactory Singleton?
Answer # SessionFactory is also thread safe so only one thread can execute at a time its code. The instance of sessionFactory is heavyweighted because it contains connection, hibernate configuration, mapping files, location path so if you create number of instance of sessionFactory then your code becomes very heavy.
Question # 15 What are the configuration files in hibernate?
Answer # Hibernate also requires a set of configuration settings related to database and other related parameters. All such information is usually supplied as a standard Java properties file called hibernate.properties, or as an XML file named hibernate.cfg.xml.
Hibernate Interview Questions And Answers For Experienced
Questions # 16 What is the use of dialect in hibernate?
Answer # Dialect means “the variant of a language”. Hibernate, as we know, is database agnostic. It can work with different databases. However, databases have proprietary extensions/native SQL variations, and set/sub-set of SQL standard implementations. Therefore at some point hibernate has to use database specific SQL.
Question # 17 What is the use of Show_sql in hibernate?
Answer # Hibernate has build-in a function to enable the logging of all the generated SQL statements to the console. You can enable it by add a “show_sql” property in the Hibernate configuration file “ hibernate.cfg.xml “. This function is good for basic troubleshooting, and to see what’s Hibernate is doing behind.
Question # 18 What is hibernate proxy and how it helps in lazy loading?
Answer # A proxy is a subclass implemented at runtime. Hibernate creates a proxy (a subclass of the class being fetched) instead of querying the database directly, and this proxy will load the “real” object from the database whenever one of its methods is called.
Question # 19 Is session is thread safe in hibernate?
Answer # SessionFactory is Hibernates concept of a single datastore and is threadsafe so that many threads can access it concurrently and request for sessions and immutable cache of compiled mappings for a single database. Why to make session object thread safe if we already have a SessionFactory(immutable) object.
Hibernate Interview Questions And Answers For 2 Years Experience
Question # 20 What is the use of configuration in hibernate?
Answer # The org.hibernate.cfg.Configuration is used to build an immutable org.hibernate.SessionFactory . The mappings are compiled from various XML mapping files. A org.hibernate.cfg.Configuration also allows you to specify configuration properties.
Question # 21 What is criteria in hibernate?
Answer # In Hibernate, the Criteria API helps us build criteria query objects dynamically. Criteria is a another technique of data retrieval apart from HQL and native SQL queries. The primary advantage of the Criteria API is that it is intuitively designed to manipulate data without using any hard-coded SQL statements.
Question # 22 What is the difference between lazy and eager loading in hibernate?
Answer # All data is fetched when eager marked data in the object when session is connected. However, in case of lazy loading strategy, lazy loading marked object does not retrieve data if session is disconnected (after session.close() statement). All that can be made by hibernate proxy.
Question # 23 Is Sessionfactory immutable?
Answer # The internal state of a SessionFactory is immutable. Most problems with concurrency occur due to sharing of objects with mutable state. Once the object is immutable, its internal state is setted on creation and cannot be changed. So many threads can access it concurrently and request for sessions.
Question # 24 Is Hibernate configuration file mandatory?
Answer # Basically you are setting all the required properties via your properties object so there is no real need to tell Hibernate to look for a hibernate.cfg.xml file which is exactly what the configure() method does. No, it’s not mandatory to use hibernate.cfg.xml. Just don’t use .configure().
Question # 25 What is meant by annotation in hibernate?
Answer # Hibernate annotations are the newest way to define mappings without the use of XML file. You can use annotations in addition to or as a replacement of XML mapping metadata. Hibernate Annotations is the powerful way to provide the metadata for the Object and Relational Table mapping.
Hibernate Interview Questions And Answers For Experienced In Java
Question # 26 What does hibernate.hbm2ddl.auto create means?
Answer # hibernate.hbm2ddl.auto. Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop , the database schema will be dropped when the SessionFactory is closed explicitly.
Question # 27 What is the meaning of persistence in hibernate?
Answer # When a POJO instance is in session scope, it is said to be persistent i.e hibernate detects any changes made to that object and synchronizes it with database when we close or flush the session. And about hibernate.properties and XML Mapping.
Question # 28 How does Hibernate proxy work?
Answer # When a method is invoked on the object, Hibernate will fetch the data from the column and populate the object. This is the proxy mechanism. To add this new behavior (the loading of the data when a method is invoked), Hibernate will create a dynamic subclass of Person using CGLib and add the desired functionality.
Question # 29 What is first level cache in hibernate?
Answer # First level cache is associated with “session” object. The scope of cache objects is of session. First level cache is enabled by default and you can not disable it. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.
Question # 30 What is the use of Session in hibernate?
Answer # The main runtime interface between a Java application and Hibernate. This is the central API class abstracting the notion of a persistence service. The lifecycle of a Session is bounded by the beginning and end of a logical transaction. (Long transactions might span several database transactions.)
Hibernate Interview Questions And Answers For 3 Years Experience
Question # 31 What is lazy fetching in Hibernate?
Answer # Lazy fetching decides whether to load child objects while loading the Parent Object. You need to do this setting respective hibernate mapping file of the parent class. Lazy = true (means not to load child) By default the lazy loading of the child objects is true.
Question # 32 What is an SQL dialect?
Answer # SQL Dialect. The SQL dialect, derived from the Structured Query Language, uses human-readable expressions to define query statements. Use a SQL query statement with the following ADSI search interfaces: The ActiveX Data Object (ADO) interfaces, which are Automation interfaces that use OLE DB.
Question # 33 What is in HQL?
Answer # Hibernate Query Language (HQL) is an object-oriented query language, similar to SQL, but instead of operating on tables and columns, HQL works with persistent objects and their properties. HQL queries are translated by Hibernate into conventional SQL queries, which in turns perform action on database.
Question # 34 What is the use of projection in hibernate?
Answer # To put it simple, Hibernate Projections are used in order to query only a subset of the attributes of an entity or group of entities you’re querying with Criteria. You can also use Projections to specify distinct clauses and aggregate functions like max , sum and so on. Like modifying the select clause in an SQL query.
Question # 35 What is Lazyinitializationexception in hibernate?
Answer # Indicates access to unfetched data outside of a session context. For example, when an uninitialized proxy or collection is accessed after the session was closed.
Question # 36 What are the different cascade types in hibernate?
Answer #
JPA EntityManager action – JPA CascadeType – Hibernate native Session action – Hibernate native CascadeTypedetach(entity) DETACH evict(entity) DETACH or EVICT merge(entity) MERGE merge(entity) MERGEpersist(entity) PERSIST persist(entity) PERSISTrefresh(entity) REFRESH refresh(entity) REFRESHremove(entity) REMOVE delete(entity) REMOVE or DELETE saveOrUpdate(entity) SAVE_UPDATE replicate(entity, replicationMode) REPLICATElock(entity, lockModeType) buildLockRequest(entity, lockOptions) LOCK
Question # 37 What is the use of Show_sql in hibernate?
Answer # Hibernate has build-in a function to enable the logging of all the generated SQL statements to the console. You can enable it by add a “show_sql” property in the Hibernate configuration file “ hibernate.cfg.xml “.
This function is good for basic troubleshooting, and to see what’s Hibernate is doing behind.
Question # 38 What is hbm2ddl in hibernate?
Answer # hibernate.hbm2ddl.auto Automatically validates or exports schema DDL to the database when the SessionFactory is created. With create-drop, the database schema will be dropped when the SessionFactory is closed explicitly.
Question # 39 What is the use of dialect in hibernate?
Answer # Dialect means “the variant of a language”. Hibernate, as we know, is database agnostic. It can work with different databases. However, databases have proprietary extensions/native SQL variations, and set/sub-set of SQL standard implementations. Therefore at some point hibernate has to use database specific SQL.
Question # 40 Can I disable first level cache in hibernate?
Answer # The scope of cache objects is of session. Once session is closed, cached objects are gone forever. First level cache is enabled by default and you can not disable it. When we query an entity first time, it is retrieved from database and stored in first level cache associated with hibernate session.
Hibernate Interview Questions And Answers For 4 Years Experience
Question # 41 What is criteria in hibernate?
Answer # In Hibernate, the Criteria API helps us build criteria query objects dynamically. Criteria is a another technique of data retrieval apart from HQL and native SQL queries. The primary advantage of the Criteria API is that it is intuitively designed to manipulate data without using any hard-coded SQL statements.
Question # 42 Which one is faster JDBC or hibernate?
Answer # JDBC will always give better performance as compared to Hibernate for most of the database vendors. The choice of hibernate over jdbc and sql queries is not because of the performance but because of reasons mainly object persistence and database independence in terms of not writing database specific queries.
Question # 43 What is the use of bag in hibernate?
Answer # Hibernate Bag is a java collection that stores elements without caring about the sequencing, but allow duplicate elements in the list. A bag is a random grouping of the objects in the list.
Question # 44 What is the use of Mappedby in hibernate?
Answer # With the mappedBy , you directly tell Hibernate/JPA that one table owns the relationship, and therefore it is stored as a column of that table. Without, the relationship is external and Hibernate/JPA need to create another table to store the relationship.
Question # 45 What is inverse true in hibernate?
Answer # The real meaning is that it defines which side is the parent or the relationship owner for the two entities (parent or child). Hence, inverse=”true” in a Hibernate mapping shows that this class (the one with this XML definition) is the relationship owner; while the other class is the child.
Question # 46 What is a bidirectional relationship?
Answer # Bidirectional Relationships. In a bidirectional relationship, each entity has a relationship field or property that refers to the other entity. Through the relationship field or property, an entity class’s code can access its related object.
Question # 47 What is the dirty checking in hibernate?
Answer # Hibernate allows dirty checking feature.All persistent objects are monitored by hibernate.it detects which objects have been modified and then calls update statements on all updated objects.the process of updating the changed object is called automatic dirty checking.
Question # 48 What are the important interfaces in hibernate?
Answer #
Session InterfaceSession Factory InterfaceConfiguration InterfaceTransaction InterfaceQuery and Criteria Interface
Question # 49 What is hibernate mapping file?
Answer # The mapping document is an XML document having <hibernate-mapping> as the root element, which contains all the <class> elements. The <class> elements are used to define specific mappings from a Java classes to the database tables.
Question # 50 What is difference between openSession() and getCurrentSession()?
Answer # If you set hibernate.current_session_context_class to thread and then implement something like a servlet filter that opens the session – then you can access that session anywhere else by using the SessionFactory.getCurrentSession().
SessionFactory.openSession() always opens a new session that you have to close once you are done with the operations.
SessionFactory.getCurrentSession() returns a session bound to a context – you don’t need to close this.
Hibernate Interview Questions And Answers For 5 Years Experience
Question # 51 What is difference between Session get() and load() method in Hibernate?
Answer #
Session.load() – It will always return a “proxy” (Hibernate term) without hitting the database. In Hibernate, proxy is an object with the given identifier value, its properties are not initialized yet, it just look like a temporary fake object. If no row found , it will throws an ObjectNotFoundException.
Session.get() – It always hit the database and return the real object, an object that represent the database row, not proxy.If no row found , it return null.
Question # 52 What are different states of an entity bean in Hybernate?
Answer # The Entity bean has three states:
TransientPersistentDetached
Transient: When ever we create a new object of Entity bean then we can say that is in Transient state,At that time any modification in the object state does not effect on database.
Persistent: When ever the Object of entity bean associated with session we can say that is in persistent state, if any change in the object state , then that modification effects in database.
Detached :When ever the object is removed from session then it enters in to detached state.Any modification on detached state object , does not effect in database.
Question # 53 Difference between hibernate session merge() vs update()?
Answer # Hibernate handles persisting any changes to objects in the session when the session is flushed. update can fail if an instance of the object is already in the session. Merge should be used in that case. It merges the changes of the detached object with an object in the session, if it exists.
Question # 54 Difference between save() and saveorupdate() in hibernate?
Answer # The important difference between the org.hibernate.Session class methods, save & saveOrUpdate is, save generates a new identifier and results in an INSERT query, whereas saveOrUpdate does an INSERT or an UPDATE. Save method stores an object into the database.
Question # 55 What is the difference between save() and persist() in hibernate?
Answer # persist() – Hibernate persist is similar to save (with transaction) and it adds the entity object to the persistent context, so any further changes are tracked. If the object properties are changed before the transaction is committed or session is flushed, it will also be saved into database.
persist() also guarantees that it will not execute an INSERT statement if it is called outside of transaction boundaries. This is useful in long-running conversations with an extended Session/persistence context.
save() 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, not “sequence”), this INSERT happens immediately, no matter if you are inside or outside of a transaction. This is not good in a long-running conversation with an extended Session/persistence context.
Hibernate Tricky Interview Questions For Experienced
Question # 56 What are the collection types in Hibernate?
Answer # Hibernate collections types are:
java.util.List.java.util.Set.java.util.SortedSet.java.util.Map.java.util.SortedMap.java.util.Collection.or write the implementation of org.hibernate.usertype.UserCollectionType.
Question # 57 How to implement Joins in Hibernate?
Answer # Using HQL we can implement joins in hibernate.
HQL Joins – HQL supports inner join, left outer join, right outer join and full join. For example, select e.name, a.city from Employee e INNER JOIN e.address a . In this query, Employee class should have a variable named address.
Question # 58 What is Named SQL Query?
Answer # A named query is a SQL expression represented as a table. In a named query, you can specify an SQL expression to select rows and columns returned from one or more tables in one or more data sources.
Question # 59 What is Hibernate Criteria API?
Answer # In Hibernate, the Criteria API helps us build criteria query objects dynamically. Criteria is a another technique of data retrieval apart from HQL and native SQL queries. The primary advantage of the Criteria API is that it is intuitively designed to manipulate data without using any hard-coded SQL statements.
Question # 60 What are callback interfaces in hibernate?
Answer # Callback interface for Hibernate code. To be used with HibernateTemplate ‘s execution methods, often as anonymous classes within a method implementation. The typical implementation will call Session.load/find/update to perform some operations on persistent objects.
Hibernate Tough Interview Questions And Answers For Experienced
Question # 61 What is transaction management in hibernate?
Answer # In a non-managed environment, Hibernate is usually responsible for its own database connection pool. The application developer has to manually set transaction boundaries (begin, commit, or rollback database transactions) themselves.
Question # 62 What are the mapping associations used in hibernate?
Answer # There are two mapping associations used in hibernate, they are:
1) One-to-One Association2) Many-to-Many Association
Question # 63 What is Hibernate QBC API?
Answer # Hibernate Query By Criteria (QBC) API is used to create queries by manipulation of criteria objects at runtime.
Question # 64 What is hibernate criteria join?
Answer # Hibernate Criteria JOIN API allows users to perform join operation.
Suppose you have to perform a operation like
SELECT S.*, C.* FROM STUDENT S, CONTACT C WHERE S.ROLL_NO=C.ID;
Then you can write this statement using Criteria join in a very simple way
Criteria criteria = session.createCriteria(Student.class);criteria.setFetchMode(“Contact”, FetchMode.JOIN);List list = criteria.list();
Question # 65 What is the is the default transaction factory in hibernate?
Answer # JDBCTransactionFactory is the default transaction factory in hibernate.
Hibernate Real Time Interview Questions And Answers For Experienced
Question # 66 What is JMX in Hybernate?
Answer # Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices (such as printers) and service-oriented networks. Those resources are represented by objects called MBeans (for Managed Bean).
Question # 67 How to bind hibernate session factory to JNDI?
Answer # When binding the SessionFactory to JNDI, Hibernate will use the values of hibernate.jndi.url , hibernate.jndi.class to instantiate an initial context.
Question # 68 What are the fetching strategies in hibernate?
Answer # There are four fetching strategies
1. fetch-“join” = Disable the lazy loading, always load all the collections and entities.
2. fetch-“select” (default) = Lazy load all the collections and entities.
3. batch-size=”N” = Fetching up to ‘N’ collections or entities, *Not record*.
4. fetch-“subselect” = Group its collection into a sub select statement.
Question # 69 What are derived properties in hibernate?
Answer # In Hibernate a derived property (also called a calculated property) is a read-only property whose value is calculated at fetch time using SQL expressions.
Question # 70 What is version property in hibernate?
Answer # The <version> property (or @Version annotation) – We know that that Hibernate can provide optimistic locking through a version property on your persistent objects. Furthermore, the version property is automatically managed by Hibernate.
Advanced Hibernate Interview Questions And Answers For Experienced
Question # 71 What does session lock () method do in hibernate?
Answer # The lock() method, with LockOptions.NONE, can be used to associate a detached object to a session and put the object back into a persistence context. On top is Hibernate code to reattach a detached object using a typical update method call. On the bottom is code to reattach a detached object using a lock method call.
Question # 72 What does evict do in hibernate?
Answer # evict() To detach the object from session cache, hibernate provides evict() method. After detaching the object from the session, any change to object will not be persisted. The associated objects will also be detached if the association is mapped with cascade=”evict”.
Question # 73 What is implicit polymorphism in hibernate?
Answer # Implicit polymorphism means if a class or interface is used in HQL, criteria or named queries, hibernate fetches the records from the table mapped to the used class along with all the tables mapped to its subclasses, at any hierarchy level. This is one of the great advantage for using hibernate.
Question # 74 What is table per concrete class in hibernate?
Answer # When we use Table Per Concrete class in hibernate, tables are created per class. So there are no nullable values in the table. Disadvantage of this approach is that duplicate columns are created in the subclass tables.
Question # 75 What is light object mapping in hibernate?
Answer # Light Object Mapping is one of the levels of ORM quality in which all entities are represented as classes and they are mapped manually in the Relational Tables.
The code is hidden from the business logic by implementing specific design patterns. In this Light Object Mapping in Hibernate is one of the valuable levels of ORM quality that approach for successful application which having less number of entities, or for common applications, Metadata and driven data models.
Must Read Java Interview Questions Books
RELATED INTERVIEW QUESTIONS
- ASP.NET Interview Questions
- PHP Interview Questions
- Kubernetes Interview Questions
- Docker Interview Questions
- CEH Interview Questions
- CyberArk Interview Questions
- Appian Interview Questions
- Drools Interview Questions
- Talend Interview Questions
- Selenium Interview Questions
- Ab Initio Interview Questions
- AB Testing Interview Questions
- Mobile Application Testing Interview Questions
- Pega Interview Questions
- UI Developer Interview Questions
- Tableau Interview Questions
- SAP ABAP Interview Questions
- Reactjs Interview Questions
- UiPath Interview Questions
- Automation Anywhere Interview Questions
- RPA Interview Questions
- RPA Blue Prism Interview Questions
- Ranorex Interview Questions
- AWS Interview Questions
- SSRS Interview Questions
- SQL Interview Questions
- Informatica MDM Interview Questions
- CyberArk Interview Questions
- SAP SD Interview Questions
- SAP EWM Interview Questions
I believe that is among the so much important information for me.
And i’m satisfied studying your article. However want to commentary on few
common issues, The website style is perfect, the
articles is in point of fact great : D. Excellent process, cheers
Very helpful content.