Hibernate face Test

Source: Internet
Author: User

The difficulty is: Session Management and performance optimization. (Performance optimization is an experience; for session management, simple hibernate can be solved using threadlocal, and if it is combined with spring, it is good to use the session management scheme provided by spring.) )

The difference between 1.load () and get ()

Both will look for objects in the cache first, first level cache, and then level two cache. When the object is not found in the cache, if we use the Session.load (User.class,id) method to load an object, the SQL statement is not emitted at this time, the current object is actually a proxy object, this proxy object only holds the ID value of the Entity object, It is only when we want to use this object that we get other properties that we issue an SQL statement to query our object from the database. This proxy object exists in the first level cache, in the form of a map, when the ID of Map key;get () will be left to check the database. This can significantly improve performance in some cases.

If the data is not found in the database, get () throws an nullpointexception exception, and when the load () method is executed, a Objectnotfoundexception exception is thrown by using the proxy object. When we just see the console report lazyinitializationexception this exception, we know that load is used to delay the loading of an object (perhaps after the proxy object has not yet used the proxy object, that is, to actually check the database, the database is closed. ), there are two ways to solve this, one is to change the load to get the object, and the other is to open our session and close the session in the presentation layer.

2.What is the difference between sorted collection and ordered collection?

Sorted collection is sorted in memory by the Java comparer
Ordered collection are sorted in the database through order by

3. compare the advantages and disadvantages of Hibernate's three retrieval strategies

1 immediate retrieval;
Advantage: The application is completely transparent to the application, regardless of whether the object is in a persistent state or in a free State, the applications can easily navigate from one object to the object associated with it;
Cons: Too many 1.select statements; 2. It is possible to load objects that the application does not need to access, wasting a lot of memory space;
2 Deferred retrieval:
Pros: The application determines which objects need to be loaded, avoids the ability to execute redundant SELECT statements, and avoids loading objects that the application does not need to access. Therefore can improve the retrieval performance, and can save memory space;
Disadvantage: If the application wants to access the Free State proxy class instance, it must ensure that he has been initialized in the persistent state;
3 Urgent left Outer connection search
Pros: 1 The application is completely transparent, regardless of whether the object is persisted or in a free State, the application can easily navigate an object to the object associated with it. 2 The number of SELECT statements is small by using outer joins;
Cons: 1 May load objects that the application does not need to access, wasting a lot of memory space; 2 Complex database table connections can also affect retrieval performance;

4. How to see the SQL that hibernate generates and executes in the console

In the file applicationconfig.xml that defines the database and database properties, set Hibernate.show_sql to True
The resulting SQL will appear in the console
Note: Doing so can add to the burden of the system, which is not conducive to performance tuning

5.What caching policies are supported by Hibernate

Read-only: This strategy applies to data that is frequently read but not updated, which is by far the simplest and most efficient caching strategy
Read/write: This strategy applies to data that needs to be updated, is more resource-intensive than read-only, and in a non-JTA environment, each transaction needs to be called in Session.close and Session.disconnect ()
Nonstrict Read/write: This strategy does not guarantee that two simultaneous transactions will modify the same piece of data, which applies to data that is often read but rarely updated
Transactional: This strategy is a fully transactional cache policy that can be used in a JTA environment

6. HowHibernate works and why it should be used

Principle:
1. Read and parse the configuration file
2. Read and parse mapping information, create Sessionfactory
3. Open Sesssion
4. Create Transaction Transation
5. Persistent operation
6. Commit a transaction
7. Close session
8. Close Sesstionfactory

Why to 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 JDBC-based, mainstream persistence framework that is an excellent ORM implementation. He simplifies the coding of the DAO layer to a great extent.

3. Hibernate uses the Java reflection mechanism rather than the bytecode enhancer to achieve transparency.

4. 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.

7.How do I implement the relationship between classes in Hibernate? such as: one-to-many, many-to-many relationships)

The relationship between classes and classes is mainly manifested in the relationship between tables and tables, all of which operate on objects, and all tables and classes are mapped together in our program, through Many-to-one, One-to-many, many-to-many in the configuration file.

8.Hibernate's caching mechanism

1. The internal cache exists in Hibernate, also called the first level cache session, belongs to the application thing-level cache.

2. Level Two cache:
A) application and caching
b) Distributed cache
Conditions: Data is not modified by third parties, data size is acceptable, data update frequency is low, the same data is used by the system, non-critical data.

The sessionfactory cache can be divided into two categories: built-in caches and external caches. The session cache is built-in and cannot be uninstalled, and is also known as Hibernate's first-level cache. Sessionfactory's built-in cache and session cache are similar in implementation, which is the data contained in some of the collection properties of the Sessionfactory object, which refers to the data contained in some of the session's collection properties.  Sessionfactory's built-in cache contains mapping metadata and pre-defined SQL statements, mapping metadata is a copy of the data in the mapping file, and the predefined SQL statements are derived from the mapping metadata during the hibernate initialization phase, sessionfactory The built-in cache is read-only and the application cannot modify the mapping metadata and predefined SQL statements in the cache, so sessionfactory does not need to synchronize the built-in cache with the mapped file. Sessionfactory's external cache is a configurable plug-in. By default, Sessionfactory does not enable this plugin. The external cached data is a copy of the database data, and the externally cached media can be either memory or hard disk. Sessionfactory's external cache is also known as Hibernate's second-level cache.

3. Implementation of third-party caches

9. How to optimize hibernate

1. Use bidirectional one-to-many associations without using
2. Flexible use of unidirectional one-to-many associations
3. Do not use one-to-many substitution
4. Configure the object cache without using the collection cache
5. One-to-many collection using bag, multi-set using Set
6. Inheriting classes using explicit polymorphism
7. Table fields are less, tables associated not afraid of more, there is a level two cache backing

Hibernate face Test

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.