Hibernate summary of common interview questions in Java

  • 2020-04-01 03:59:32
  • OfStack

Mainly from the following ten aspects do summary to Hibernate, including Hibernate retrieval way, Hibernate in the state of an object, the three retrieval strategy is what Hibernate, respectively suitable for what kind of situation, don't match the ORM to solve problems, Hibernate mapping inheritance relationships of three ways, the Session of the find () method and the difference between the Query interface and so on the summary of the problem, specific content is as follows:

1   Hibernate retrieval method

& Oslash;   Navigation object graph retrieval (navigation to other objects based on the objects already loaded).

& Oslash;   OID retrieval (retrieval of objects by their OID.)

& Oslash;   HQL retrieval (using the object-oriented HQL query language).

& Oslash;   QBC retrieval (using the QBC(Qurey By Criteria)API to retrieve objects. QBC/QBE offline/online)

& Oslash;   Local SQL retrieval (SQL queries using the local database).

What are the three retrieval strategies of Hibernate, the state of the object in Hibernate, what are the three retrieval strategies of Hibernate, what are they applicable to, the mismatch problem solved by ORM, the three methods of Hibernate mapping inheritance relationship, the find() method of Session and the difference of Query interface
& Oslash;   Transient state: just created with a new statement, not persisted, not in Session cache. A Java object in a temporary state is called a temporary object.

& Oslash;   Persistent state (persistent) : it has been persisted and added to the Session cache. A Java object in a persistent state is called a persistent object.

& Oslash;   The free state (detached) : has been persistent, but is no longer in the Session cache. A Java object in a free state is called a free object.
3   What are the three retrieval strategies of Hibernate and what are they applicable to
& Oslash;   Immediately to retrieve

Advantage: full transparency to the application.

Disadvantages: large number of select statements.

Applicable: class level.

& Oslash;   Delay retrieval

Advantages: it is up to the application to decide which objects to load, avoiding the execution of redundant select statements and the loading of objects that do not need to be accessed, saving memory space and improving retrieval efficiency.

Disadvantages: if an application wants to access an instance of a proxy class in the free state, it must ensure that it is initialized during persistence.

Applicable: one-to-many or many-to-many associations. Objects that the application does not need to access immediately or at all.

& Oslash;   Urgent left outer connection retrieval

Advantages: completely transparent to the application, enabling the application to easily navigate from one object to another that is associated with it, regardless of whether the object is persistent or free. External joins are used and the number of select statements is small.

Disadvantages: it may load objects that the program is not allowed to access. Complex database table join image retrieval performance.

Applicable: one-to-one or many-to-one correlation. Objects that the application needs to access immediately. The database system has good table join performance.

4   Mismatch problem solved by ORM (mismatch between domain model and relational model)
& Oslash;   The domain model is object-oriented and the relational model is relational.

& Oslash;   There is an inheritance relation in the domain model, and the inheritance relation cannot be directly represented in the relational model.

& Oslash;   In the domain model, there are many-to-many association relationships, which are represented by joining tables.

& Oslash;   In the domain model there is a bidirectional correlation, in the relational model there is only a one-way reference, and there are always many references to one side.

& Oslash;   Domain models advocate fine-grained models, and relational models advocate coarse-grained models.

5   Hibernate maps inheritance relationships in three ways
& Oslash;   The entire inheritance system USES one tableper hierarchy.

& Oslash;   A table for each subclass that holds attributes specific to the subclass (tableper subclass)

& Oslash;   One table for each concrete class (union-subclass), which is saved as the complete information of the subclass (table per concrete).

6   The difference between the find() method of Session and the Query interface
Both the find() method of the Session class and the Query interface support HQL retrieval. The difference between the two is that the former is just a convenient way to execute some simple HQL queries, it doesn't have the ability to dynamically bind parameters, and the find() method has been phased out in hibernate3.x. The Query interface is the true HQL Query interface, which provides the various Query capabilities listed above.

7   Hibernate association relationship configuration
& Oslash;   One to one

& Oslash;   More than a pair of

& Oslash;   Many to many

8   Briefly describe the characteristics of Session
& Oslash;   It is not thread-safe, so when designing software architectures, you should avoid multiple threads sharing the same Session instance.

& Oslash;   Session instances are lightweight, meaning that they are created and destroyed without consuming too many resources. This means that Session objects can often be created or destroyed in the program, such as assigning a separate Session instance to each client request or a separate Session instance to each unit of work.

& Oslash;   In Session, each database operation is performed in a transaction, so that different operations (even read-only operations) can be isolated.

9   Advantages of using XML files to configure object-relational mapping in Hibernate
Hibernate does not penetrate either the upper domain model or the lower data model. Software developers can design domain models independently without being forced to conform to any specification. Database designers can design data models independently, without being forced to conform to any specification. Object-relational mapping does not depend on any program code. If you need to modify the object-relational mapping, you only need to modify the XML file without modifying any program, which increases the flexibility of the software and makes maintenance easier.

10   Caching of sessions
& Oslash;   Reduce the frequency of database access. Applications can obviously read persistent objects from memory much faster than they can query data in the database, so the caching of sessions can improve the performance of data access.

& Oslash;   Ensure that objects in the cache are synchronized with related records in the database. When the state of the persistent objects in the cache changes, the Session does not immediately execute the relevant SQL statements, which enables the Session to combine several related SQL statements into one SQL statement to reduce the number of times the database is accessed, thereby improving the performance of the application.

11   Problems with concurrent multi-transaction runtimes
& Oslash;   The first type loses updates

& Oslash;   Dirty read

& Oslash;   Phantom reading/phantom reading

& Oslash;   Unrepeatable reading

& Oslash;   The second type loses updates

12   The difference between clearing and emptying sessions
& Oslash;   Session cleaning cache refers to synchronously updating the database according to the state changes of the objects in the cache. (Hibernate contains Flush mode with three types of sessions)

& Oslash;   Clearing is session clearing some or all objects or session closing;

13   How does Hibernate work and why
& Oslash;   The principle of

1. Read and parse the configuration file

2. Read and parse the mapping information to create a SessionFactory

3. Open the Sesssion

4. Create transaction Transation

5. Persist operations

6. Commit transactions

7. Close the Session

8. Close SesstionFactory

& Oslash;   Why use

1. The code for JDBC access to the database is encapsulated, which greatly simplifies the tedious repetitive code of the data access layer.

2.Hibernate is a mainstream persistence framework based on JDBC and an excellent ORM implementation. It greatly simplifies the coding of the DAO layer

Hibernate USES Java reflection instead of bytecode enhancers to achieve transparency.

Hibernate performs very well because it is a lightweight framework. The flexibility of the mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.

14   Hibernate lazy loading
1.Hibernate2 lazy load implementation: a) entity object b) Collection type

2.Hibernate3 provides lazy loading of properties

When Hibernate queries data, the data does not exist in memory. When the program actually operates on the data, the object exists in memory, which realizes lazy loading. It saves the memory overhead of the server and thus improves the performance of the server.

15   Hibernate's caching mechanism
1. The first-level cache, also called internal cache, exists in Hibernate and belongs to the application transaction level cache

2. Level 2 cache

A) application and caching

B) distributed caching

Conditions: the data will not be modified by the third party, the data size is in the acceptable range, the data update frequency is low, the same data is frequently used by the system, non-critical data.

C) implementation of third-party cache

16   Optimize Hibernate
1. Use bidirectional one-to-many association instead of one-way one-to-many

2. Use one-way one-to-many associations flexibly

3. Instead of one-to-one, use many-to-one

4. Configure the object cache instead of using the collection cache

5. Use Bag for one-to-many sets and Set for many-to-many sets

6. Inheritance classes use explicit polymorphism

7. The table field should be less, the table association should not be afraid of more, there are two levels of cache support

The above is for Java interview more common Hibernate summary, I hope to be able to help you.


Related articles: