Hibernate Summary of Java interview FAQ _java

Source: Internet
Author: User
Tags inheritance

Mainly from the following more than 10 aspects of Hibernate summary, including the hibernate of the search method, hibernate the status of the object, hibernate of the 3 kinds of search strategies, which are applicable to the situation, ORM solve the mismatch problem, Hibernate mapping inheritance relationship of 3 ways, the session of the Find () method and query interface differences and other aspects of the summary, the specific contents are as follows:

1 Hibernate Search method

Ø Navigation Object graph retrieves (navigates to other objects based on objects that have already been loaded). )

Øoid retrieves the object by the OID of the object. )

ØHQL retrieval using an object-oriented hql query language. )

ØQBC retrieves (using the QBC (Qurey by Criteria) API to retrieve objects. QBC/QBE offline/online)

Ø Local SQL retrieval (using a local database SQL query statement.) )

Including Hibernate search methods, hibernate in the state of objects, hibernate 3 kinds of search strategy is what, respectively applicable to the situation, ORM solve the mismatch problem, hibernate mapping inheritance relationship 3 ways, session of Find () Methods and the differences between query interface and so on.
Ø temporary State (transient): has just been created with the new statement, has not been persisted, is not in the session cache. A Java object that is in a temporary state is called a temporary object.

Ø Persistent State (persistent): Has been persisted and added to the session cache. A Java object that is in a persistent state is called a persisted object.

Ø Free State (detached): Has been persisted, but is no longer in the session's cache. A Java object in a free state is called a free object.
3 Hibernate 3 kinds of search strategies, which are suitable for each occasion
Ø Immediate Search

Benefits: Fully transparent to the application.

Disadvantage: The number of SELECT statements is large.

Applicable: Class level.

Ø Delayed retrieval

Benefits: The application decides which objects to load, avoids executing redundant SELECT statements, and avoids loading unwanted objects, saves memory space, and improves retrieval efficiency.

Disadvantage: If an application wants to access a proxy class instance of a free State, it must be guaranteed to be initialized when it is persisted.

Applies to: One-to-many or many-to-many associations. An application does not require immediate access or an object that will not be accessed at all.

Ø urgent left OUTER JOIN search

Advantages: Fully transparent to the application, regardless of whether the object is in a persistent state or a free State, the application can easily navigate from one object to another object associated with it. With an outer join, the number of SELECT statements is small.

Disadvantage: The object that the program is not allowed to access may be loaded. Complex database table connection image retrieval performance.

Applicable: One-to-one or Many-to-many association. An object that the application requires immediate access to. The database system has good table connection performance.

4 ORM-Resolved mismatch problem (mismatch between domain model and relational model)
the domain model is object oriented, and the relational model is oriented to the relationship.

There is an inheritance relationship in the domain model, and the inheritance relationship cannot be directly represented in the relational model.

There are many pairs of correlation relationships in the domain model, and the relationship model expresses the Many-to-many association relationship by connecting table.

Ø in the domain model, there are bidirectional association relations, only one-way reference relationship in the relational model, and always many reference one side.

Ø The domain model advocates the granular model, and the relational model advocates the coarse-grained model.

5 Hibernate mapping inheritance relationships in 3 ways
Ø the whole system of inheritance is a table (Tableper hierarchy)

Ø a table for each subclass that holds the attributes peculiar to the subclass (Tableper subclass)

Ø each specific Class A table (Union-subclass), save is the subclass complete information (table per concrete)

6 Session of the Find () method and query interface differences
The Find () method of the session class and the query interface support HQL retrieval. The difference is that the former is just a convenient way to execute some simple HQL query statements, it does not have the function of dynamic binding parameters, and in the hibernate3.x version, has eliminated the Find () method, and the query interface is the real HQL query interface, It provides the various query features listed above.

7 Hibernate Association Relationship Configuration
Ø One-to-one

o A pair of many

Ø many to many

8 briefly describe the characteristics of the session
Ø is not thread-safe, so when designing a software architecture, you should avoid multiple threads sharing the same session instance.

The Øsession instance is lightweight, and the so-called lightweight means that it does not need to consume too much resources to create and destroy. This means that session objects can be created or destroyed frequently in a program, such as assigning a separate session instance to each client request, or assigning a separate session instance to each unit of work.

Ø in session, each database operation is performed in a single transaction (transaction), so that different operations (even read-only) can be separated from each other.

9 Hibernate the advantages of using XML file to configure object-relational mapping
Hibernate neither penetrates into the upper-level domain model nor penetrates into the underlying data model. Software developers can design the domain model independently, without having to enforce compliance with any specifications. Database designers can design data models independently, without having to enforce compliance with any specifications. Object-relational mapping does not depend on any program code, if you need to modify the object-relationship mapping, just modify the XML file, do not need to modify any programs, improve the flexibility of the software, and make maintenance more convenient.

The role of the cache in session
Ø reduce the frequency of access to the database. The speed at which an application reads persisted objects from memory is significantly faster than querying data in the database, so the session cache can improve the performance of data access.

Ø ensure that the objects in the cache are synchronized with the related records in the database. When the state of the persisted object in the cache has been transformed, the session does not immediately execute the associated SQL statement, which allows the session to combine several related SQL statements into one SQL statement to reduce the number of accesses to the database, thereby improving the performance of the application.

11 Multi-transaction Concurrency Runtime problems
Ø First Class loss update

o Dirty Read

Ø Virtual read/Phantom read

Ø cannot be read repeatedly

Ø Second Class loss update

The difference between cleaning and emptying of session
Øsession cleanup Caching is the synchronization of updating the database according to the state of the objects in the cache; (Hibernate contains the three class session flush mode)

Ø empty is the session clears some or all objects or session closes;

How hibernate works and why you should use it
Ø principle

1. Read and resolve configuration files

2. Read and parse mapping information, create Sessionfactory

3. Open Sesssion

4. Create Transaction Transation

5. Persistent operation

6. Submission of services

7. Close session

8. Close Sesstionfactory

Ø Why to use

1. The code for JDBC access to the database has been encapsulated, simplifying the cumbersome repetitive code of the data access layer.

2.Hibernate is a mainstream persistence framework based on JDBC and is an excellent ORM implementation. He simplifies the coding of the DAO layer to a large extent

3.hibernate uses a Java reflection mechanism rather than a bytecode enhancement program to achieve transparency.

4.hibernate performance is very good because it is a lightweight framework. The flexibility of mapping is excellent. It supports a variety of relational databases, from one-to-one to many-to-many complex relationships.

Hibernate delay Loading
1.hibernate2 deferred load implementation: a) Entity object B collection type (Collection)

2.hibernate3 provides deferred loading of properties

When the hibernate is querying the data, the data does not exist in memory, when the program is actually operating on the data, the object exists and in memory, it realizes the delay loading, saves the memory cost of the server, and improves the performance of the server.

Caching mechanism of Hibernate
1. The first level cache is also called the internal cache exists hibernate, belongs to the application transaction level cache

2. Level Two cache

A) application and caching

b) Distributed caching

Conditions: The data will not be modified by the third party, the data size 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 caching

16 Optimization Hibernate
1. Use bi-directional One-to-many Association, do not use one-way One-to-many

2. Flexible use of one-way One-to-many Association

3. No one-on-one, replace with multiple pairs

4. Configure object caching, do not use collection caching

5. A one-to-many set uses the bag, and Many-to-many sets use the Set

6. Inheritance class uses explicit polymorphism

7. Fewer table fields, less fear of tables association, and two cache-backed

The above content is for Java interview more common hibernate summary, hope to be able to help everybody.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.