Some learning experience summary about Hibernate

  • 2020-04-01 02:09:40
  • OfStack

For Hibernate just learned a week time, as a Java beginner, also have some of their own feelings to share, if this article can be lucky to be seen by you, also just for your entertainment. If there are any shortcomings, we welcome you to give advice, a lot of criticism. For reference only, don't spray if you don't like.

Some time ago, I just learned to use JDBC to connect Java and database, to achieve the data persistence operation and add, delete, change and check, but after learning, I felt that JDBC was too tedious, because it could not directly face the object, the development efficiency, the code is too much, but also repeated, completely inconsistent with the Java object-oriented thinking mode. Hibernate was born to solve this problem for Java programmers, so we can forget about JDBC and see how Hibernate implements Java persistence.

Hibernate is an excellent Java persistence layer solution and a mainstream object-relational mapping (ORM) tool today. It has three advantages. First, it is an object relational mapping framework for developing source code. Secondly, JDBC is encapsulated with very lightweight objects, which simplifies the tedious coding of JDBC. Third: mapping JavaBean objects to database tables. ORM is a persistence layer solution, it is the class objects in Java and related properties and related classes and related to the database table and the table properties and keys to do a one-to-one mapping, to achieve the relationship between Java objects and the database. Next, I will talk about how I learned Hibernate.

One: hibernate primer
I won't say much about building a project framework and writing hibernate configuration files and entity mapping files. First of all, we want to use the Configuration interface to new Session factory first, then from the SessionFactory derives Session instance (Session factory), in general, the entire application only a SessionFactory, it should be created when the application is initialized), then get the Session instance, use the Session can operate database and object in the class, interface with the Transaction commit () and roolback () method to commit the Transaction and roll back the things, Query is used to implement queries against the database (in SQL or HQL). In addition, there are three kinds of entity objects in Hibernate: transient state, persistent state and free state when data is added, deleted, changed and checked. Different states, different implementation methods. You can take your time.

Two: relational mapping
Since Hibernate is a relational mapping tool, there must be many-to-one, one-to-many, bidirectional one-to-many, many-to-many associations. To do this, you first have an association between the entities that holds an instance of another object through one object. In the database table, the primary foreign key of the table can also realize the relation between the table and the table. We then want to represent these associations in the mapping file (hbm.xml). Many - to - one is the things should be held at the end of the one side of the object (reference), one - to - many is one side should hold the things the collection of objects, the bidirectional one-to-many is also equipped with one-way one-to-many and one-way many-to-one, many-to-many associations is to convert a many-to-many into two one-to-many, and established for the intermediate table entity class and mapping file, between two endpoints, and the established bidirectional one-to-many associations respectively.

Three: HQL practical technology
Hibernate supports two main types of queries. HQL (Hibernate Query language) Query is an object-oriented Query language, in which there is no concept of tables and fields, only the concept of classes, objects and properties, HQL is a more widely used way. Criteria queries, also known as "object queries," encapsulate the process of constructing queries in an object-oriented manner.

HQL is more consistent with Java object-oriented thinking and simpler than SQL. There are no concepts of tables and fields in HQL, just the concepts of classes, objects, and properties. For example, if you want to look up a book with "Spring" in its name, SQL: select  * from books where book_name like '%Spring%'        HQL: from Book b where b.ame like 'Spring%' SQL USES the table name books and the field book_name in the database, while HQL USES the Book class name and the name attribute of the Book, and the Book class and the books table are mapped, so it is equivalent to the operation of the database. Is it more visual?

Criteria queries are used sparingly, fetching the Criteria instance from the Session and setting the restriction method (using the query Criteria commonly used for Restrictions). This is equivalent to converting HQL statements into methods to implement queries. See oneself like, anyway I think HQL is more practical a few.

Four: lazy loading and Hibernate caching in HQL
Both HQL and Criteria queries are lazy-loaded. Some people think this is a weakness of Hibernate, but I think it is more of a genius of Hibernate. After get a Session instance, with a Book, for example, you use the get () method of the Session to get a Book, the Book is the main object, and the Book has a Type associated objects, but get Book object won't load Type associations, only when you need to use Type connection object will force loading Type associations, specifically association object and set the default associated loading plan is: Lazy loading, where the main objects are not loaded immediately when they are loaded, but do not send the SQL statement, get the data, initialize the objects and collections until they are used, and the properties of the main objects are loaded immediately by default. Of course, this approach can be forced to change. When using Criteria queries, you can add lazy = "false"   to the mapping file of the entity class. To force the load. However, do not recommend this way, because it will cause unnecessary waste of resources, efficiency is also very low. If we need to, we can force the load better when we're writing code.

In summary, this lazy loading strategy simplifies SQL statements and improves query efficiency. You can also change the loading policy depending on the user's needs.

Hibernate cache has a level 1 cache and a level 2 cache, and for a level 1 cache, its life cycle is the same as the life cycle of a Session, so you can also refer to Hibernate level 1 cache as a Session cache. Hibernate level 1 caching is a mandatory cache. With the get () method (similar to the load () method), we can get a level 1 cache of data, and then query again without the get () method, just query the data object directly. It is important to note that the get () method is loaded by id, and the list () method also places the query results in the first level cache, but it does not look for the data in the first level cache, because the list () method is not loaded by id, and iterate methods, such as Iterator< Seeker> Iter = session. CreateQuery (" from "* *"). The iterate ();   This statement simply puts the value of the ID in the iterator and, as you iterate, looks it up in the database based on the value of the ID. And this statement produces N+1 queries.

The second level cache is managed by the SessionFactory, so it is often referred to as the SessionFactory cache. Mainly applicable to less important data, so there is no in-depth understanding.

This is the end of a week of Hibernate courses, Hibernate knowledge is still a lot of later to be used in actual combat projects to better understand.


Related articles: