A summary of the interview questions for Hibernate learning

Source: Internet
Author: User

1. What are Hibernate's search methods?

① Navigation Object Graph retrieval

②oid Search

③HQL Search

④QBC Search

⑤ Local SQL Retrieval

2. What are the status of Java objects in Hibernate?

①. Temporary state (transient): Unsaved-value attribute value that is not in the Session's cache, the OID is null, or is equal to the ID

②. Persistent state (persistent): Added to the Session's cache.

③. Free State (detached): Has been persisted, but is no longer in the Session's cache.

3. What is the difference between clearing and emptying the session?

The Session.flush () method is called to clean up the cache. The Session.clear () method is used for air conditioning.

Session clean-up cache refers to updating the database synchronously by changing the state of the objects in the cache, but not emptying the cache, emptying the session cache, but not updating the database synchronously;

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

①: If the database does not have an OID specified object. Loaded by the Get method, a null is returned, and a proxy object is returned through load, if the following code throws an exception if a property of the calling object is thrown: org.hibernate.ObjectNotFoundException;

②:load supports lazy loading, and get does not support lazy loading.

5. Hibernate pros and cons

①. Advantages:

> encapsulates the code for JDBC access to the database, simplifying the tedious repetitive code of the data access layer

> Mapping flexibility, it supports a variety of relational databases, from one-to-one to many-to-many complex relationships.

> Non-invasive, good transplant

> Caching mechanism: Provide first-level cache and level two cache

②. Disadvantages:

> Unable to optimize for SQL

The use of the ORM principle in the > framework causes the configuration to be overly complex

> execution efficiency compared to native JDBC: Especially at the time of batch data processing

> does not support batch modification, deletion  

6. Describe the experience of using Hibernate for large volume updates.

The best way to execute related SQL statements directly from the JDBC API or to invoke related stored procedures

7. Hibernate's Opensessionview problem

①. Used to solve lazy loading exception, the main function is to bind the Hibernate Session and a request thread together until the full output of the page, so that the page can be guaranteed to read data when the Session is always open, if you get deferred loading object will not error.

②. Problem: If the data is processed in large batches during the business processing phase, it is possible to cause memory overflow in the first-level cache with too much memory, and the other is the connection problem: Session and database Connection are bound together, Slow business processing can also cause database connections to not be released in a timely manner, resulting in insufficient connection pool connectivity. Therefore, it is not recommended to use this approach in projects with large concurrency, consider using an urgent left OUTER join (OUTER join FETCH) or manually initializing the associated object.

③. When configuring the filter, place it in front of the STRUTS2 filter because it will exit after the page is fully displayed.

8. What is the difference between getcurrentsession () and Opensession () in Hibernate?

①. Getcurrentsession () it will first see if the Session is bound in the current thread, and if so, return directly if it is not created again. The Opensession () is a new Session directly and returns.

②. Use ThreadLocal to isolate the thread Session.

③. Getcurrentsession () automatically closes the Session when the transaction is committed, and opensession () needs to be closed manually.

9. How do I invoke native SQL?

Call the DoWork () method of the Session.

10. Say Hibernate's cache:

The hibernate cache consists of two main classes: Hibernate cache and Hibernate level two cache:

1). Hibernate cache, also known as "session Cache", is built-in and cannot be uninstalled. Because the life cycle of a Session object usually corresponds to a database transaction or an application transaction, its cache is a transaction-scoped cache. In the first level cache, each instance of a persisted class has a unique OID.

2). Hibernate level Two cache, also known as "Sessionfactory cache", is a process-wide or cluster-wide cache because the Sessionfactory object's life cycle corresponds to the entire process of the application, so hibernate level two caches are Concurrency problems are possible, so an appropriate concurrency access policy is required that provides the transaction isolation level for cached data. The second-level cache is optional and is a configurable plug-in, which, by default, is not enabled by Sessionfactory.

When hibernate accesses the data object according to the ID, it is first checked from the session level cache, and if the level two cache is configured, then it is checked from the level two cache, and if it is not found, then the database is queried, and the result is placed in the cache to delete, update and add data by ID. Updates the cache at the same time.

A summary of the interview questions for Hibernate learning

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.