JavaRush /Java Blog /Random EN /Hibernate Interview Questions
Dr-JohnZoidberg
Level 41
Киев

Hibernate Interview Questions

Published in the Random EN group
This article is a translation and improvement of foreign articles (a list of resources is listed at the end of the article). Answers to some questions are expanded, links to third-party resources are provided that contain answers that may arise during the reading process. 1) What is Hibernate? It is a framework for object-relational mapping of entities into traditional relational databases. The main features of the framework: 1) Automatic generation and updating of tables in databases; 2) Since the main queries to the database (saving, updating, deleting and searching) are presented as methods of the framework, the code that the developer writes is significantly reduced; 3) Provides the use of a SQL-like language (HQL - hibernate query language). HQL queries can be written alongside data objects (POJO classes prepared for working with a database). 2) How to Hibernatehelps with programming? Hibernate implements a number of features that greatly simplify the developer's work. — One of these features is that hibernate allows the developer to avoid writing most SQL queries (they are already implemented, you just need to use the methods that the framework provides). — Under the hood, Hibernate has a bunch of useful tools that significantly speed up the application, the most notable of which are two-level caching and fine-tuning lazy and fetch withdrawals. — Generates tables into the database itself. 3) Methods for configuring work with Hibernate. There are four ways to configure work with Hibernate: - using annotations; - hibernate.cfg.xml; - hibernate.properties; - persistence.xml. The most common configuration method: through annotations and the persistence.xml file, as for the hibernate.properties and hibernate.cfg.xml files, then hibernate.cfg.xml is more important (if the application has both files, then the settings from the hibernate.cfg file are accepted. xml). Configuration with annotations, although convenient, is not always possible; for example, if you want to have different entity configurations for different databases or for different situations, then you should use xml configuration files. 4) What key interfaces does Hibernate use? There are five key interfaces that are used in every Hibernate-related application: - Session interface; - SessionFactory interface; — Configuration interface; — Transaction interface; — Query and Criteria interfaces. 5) What is the role of the Session interface in Hibernate? Session is the main interface that is responsible for communicating with the database. It also helps create request objects to obtain persistent objects. (persistent object - an object that is already in the database; request object - an object that is obtained when we receive the result of a query in the database, it is with this that the application works). The Session object can be obtained from SessionFactory: Session session = sessionFactory.openSession(); The role of the Session interface: is a wrapper for jdbc connection to the database; (https://ru.wikipedia.org/wiki/Java_Database_Connectiv..) - is a factory for transactions (according to the official documentation transaction - allows the application to define units of work, which, in essence, means that the transaction defines the boundaries of operations associated with database). — is the custodian of the mandatory first-level cache. 6) What is the role of the SessionFactory interface in Hibernate? It is from the SessionFactory object that we obtain objects of the Session type. There is only one SessionFactory for the entire application and it is initialized when the application starts. SessionFactory caches meta-data and SQL queries that are frequently used by the application during operation. It also caches information that was received in one of the transactions and can be used in other transactions. The SessionFactory object can be obtained using the following call: SessionFactory sessionFactory = configuration.buildSessionFactory(); 7) What types of collections are provided in Hibernate? Bag, Set, List, Map, Array. 8) What is a Bag type collection and why is it used? In its implementation, the Bag collection type is very similar to Set, the difference is that Bag can store duplicate values. Bag stores an unindexed list of elements. Most tables in the database have indexes that display the position of a data element relative to one another; these indexes are represented in the table as a separate column. With object-relational mapping, the values ​​of an index column are mapped to an index in an Array, an index in a List, or a key in a Map. If you need to get a collection of objects that do not contain index data, then you can use collections of the Bag or Set type (collections contain data in unsorted form, but can be sorted according to the request). 9) What is the difference in the operation of the load(); methods? and get();? load() method; usually used when you are not sure that the requested object is already in the database. If the object is not found, then the method throws an exception. If the object is found, the method returns a proxy object, which is a link to the object located in the database (the database request has not yet been made, a kind of lazy retrieval), a direct request to the database when we We directly access the required object through a proxy object. get() method; used then, you are not 100 percent sure whether the requested object is in the database. In the case of accessing a non-existent object, the get(); method will return null. If the object is found, the get() method; will return the object itself and the database query will be made immediately. 10) What is Lazy fetching in Hibernate? The Lazy checkout type in Hibernate is associated with leaf (child) entities and defines a shared checkout policy if there is a request to check out a parent entity. A simple example: There is an entity House. It stores information about its number, street, number of apartments and information about the families that live in the apartments; these families form a child entity relative to the House entity. When we request information about a House, it may be completely unnecessary for us to know information about the families that live in it, here lazy fetching comes to our aid, which allows us to configure the House entity so that information about families is provided only upon request, this is significant clouds the request and speeds up the application. ©dev-bay Most tables in the database have indexes that display the position of a data element relative to one another; these indexes are represented in the table as a separate column. With object-relational mapping, the values ​​of an index column are mapped to an index in an Array, an index in a List, or a key in a Map. If you need to get a collection of objects that do not contain index data, then you can use collections of the Bag or Set type (collections contain data in unsorted form, but can be sorted according to the request). 9) What is the difference in the operation of the load(); methods? and get();? load() method; usually used when you are not sure that the requested object is already in the database. If the object is not found, then the method throws an exception. If the object is found, the method returns a proxy object, which is a link to the object located in the database (the database request has not yet been made, a kind of lazy retrieval), a direct request to the database when we We directly access the required object through a proxy object. get() method; used then, you are not 100 percent sure whether the requested object is in the database. In the case of accessing a non-existent object, the get(); method will return null. If the object is found, the get() method; will return the object itself and the database query will be made immediately. 10) What is Lazy fetching in Hibernate? The Lazy checkout type in Hibernate is associated with leaf (child) entities and defines a shared checkout policy if there is a request to check out a parent entity. A simple example: There is an entity House. It stores information about its number, street, number of apartments and information about the families that live in the apartments; these families form a child entity relative to the House entity. When we request information about a House, it may be completely unnecessary for us to know information about the families that live in it, here lazy fetching comes to our aid, which allows us to configure the House entity so that information about families is provided only upon request, this is significant clouds the request and speeds up the application. ©dev-bay Most tables in the database have indexes that display the position of a data element relative to one another; these indexes are represented in the table as a separate column. With object-relational mapping, the values ​​of an index column are mapped to an index in an Array, an index in a List, or a key in a Map. If you need to get a collection of objects that do not contain index data, then you can use collections of the Bag or Set type (collections contain data in unsorted form, but can be sorted according to the request). 9) What is the difference in the operation of the load(); methods? and get();? load() method; usually used when you are not sure that the requested object is already in the database. If the object is not found, then the method throws an exception. If the object is found, the method returns a proxy object, which is a link to the object located in the database (the database request has not yet been made, a kind of lazy retrieval), a direct request to the database when we We directly access the required object through a proxy object. get() method; used then, you are not 100 percent sure whether the requested object is in the database. In the case of accessing a non-existent object, the get(); method will return null. If the object is found, the get() method; will return the object itself and the database query will be made immediately. 10) What is Lazy fetching in Hibernate? The Lazy checkout type in Hibernate is associated with leaf (child) entities and defines a shared checkout policy if there is a request to check out a parent entity. A simple example: There is an entity House. It stores information about its number, street, number of apartments and information about the families that live in the apartments; these families form a child entity relative to the House entity. When we request information about a House, it may be completely unnecessary for us to know information about the families that live in it, here lazy fetching comes to our aid, which allows us to configure the House entity so that information about families is provided only upon request, this is significant clouds the request and speeds up the application. ©dev-bay direct query to the database when we directly access the required object through a proxy object. get() method; used then, you are not 100 percent sure whether the requested object is in the database. In the case of accessing a non-existent object, the get(); method will return null. If the object is found, the get() method; will return the object itself and the database query will be made immediately. 10) What is Lazy fetching in Hibernate? The Lazy checkout type in Hibernate is associated with leaf (child) entities and defines a shared checkout policy if there is a request to check out a parent entity. A simple example: There is an entity House. It stores information about its number, street, number of apartments and information about the families that live in the apartments; these families form a child entity relative to the House entity. When we request information about a House, it may be completely unnecessary for us to know information about the families that live in it, here lazy fetching comes to our aid, which allows us to configure the House entity so that information about families is provided only upon request, this is significant clouds the request and speeds up the application. ©dev-bay direct query to the database when we directly access the required object through a proxy object. get() method; used then, you are not 100 percent sure whether the requested object is in the database. In the case of accessing a non-existent object, the get(); method will return null. If the object is found, the get() method; will return the object itself and the database query will be made immediately. 10) What is Lazy fetching in Hibernate? The Lazy checkout type in Hibernate is associated with leaf (child) entities and defines a shared checkout policy if there is a request to check out a parent entity. A simple example: There is an entity House. It stores information about its number, street, number of apartments and information about the families that live in the apartments; these families form a child entity relative to the House entity. When we request information about a House, it may be completely unnecessary for us to know information about the families that live in it, here lazy fetching comes to our aid, which allows us to configure the House entity so that information about families is provided only upon request, this is significant clouds the request and speeds up the application. ©dev-bay
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION