Detailed explanation of hibernate4 quick start example

What is Hibernate

Hibernate is a lightweight ormapping framework

Ormapping principle (object relationalmapping)

Basic mapping rules for ormapping:

1: Class corresponds to table

2: The properties of the class correspond to the fields of the table

3: Class corresponds to a specific record in the table

4: A class can correspond to multiple tables, and a table can also correspond to multiple classes

5: Tables in DB can have no primary key, but the primary key field must be set in object

6: The relationship between tables (such as foreign keys) in DB is mapped into the relationship between objects

7: The number and name of attributes in object can be different from the number and name of fields defined in the table

Basic implementation of ormapping:

Using JDBC and SQL to operate the database depends on whether it is generated dynamically or written manually.

Let's think about it. Have we implemented ormapping?

What can hibernate do:

Hibernate is mainly used to realize the mapping between Java objects and tables. In addition, it also provides data query and data acquisition methods, which can greatly reduce the time of manually using SQL and JDBC to process data during development.

Hibernate aims to liberate 95% of the programming tasks related to data persistence that developers usually do. For data centric programs, they often only use stored procedures in the database to realize business logic, and Hibernate may not be the best solution; Hibernate is most useful for those applications that implement object-oriented business models and business logic in Java based middle tier applications.

Hibernate can help you eliminate or package SQL code for specific vendors, and help you convert the result set from a tabular representation to a series of objects.

A very brief high-level overview of Hibernate architecture

Hibernate runtime architecture

The "minimal" architecture scheme requires applications to provide their own JDBC connections and manage their own transactions. This scheme uses the minimal subset of Hibernate API

The "comprehensive solution" architecture scheme abstracts the application layer from the underlying JDBC / jtaapi and lets hibernate handle these details.

SessionFactory(org.hibernate.SessionFactory)

The compiled memory image of a single database mapping relationship is thread safe (immutable). It is the factory for generating sessions, and the connectionprovider itself is used.

Session(org.hibernate.Session)

A single threaded object that represents the interaction between the application and the persistent storage layer. This object has a short lifetime, hides the JDBC connection, and is also a transaction factory.

Transaction(org.hibernate.Transaction)

The object used by an application to specify the scope of the atomic operation unit. It is single threaded and has a short life cycle. It separates the application from the underlying concrete JDBC, JTA and CORBA transactions through abstraction.

ConnectionProvider(org.hibernate.connection.ConnectionProvider)

A factory that generates JDBC connections (with the function of connection pool). It separates applications from the underlying datasource or drivermanager through abstraction. It is only used by developers for extension / implementation and is not exposed to applications.

TransactionFactory(org.hibernate.TransactionFactory)

The factory that generates the transaction object instance. It is only for developers to extend / implement and is not exposed to applications.

HelloWorld

To study how to do it, we must first find out what needs to be done

According to the learning just now, to make a basic hibernate application, you should complete the following work: object, database table, two kinds of configuration files, and client program to call hibernate interface for operation.

Build environment

翻译错误 TIMEOUT

What does object do

1: It's the way you learned to write VO (the same four rules)

2: It is required that there must be a constructor with a public null parameter. At present, when writing VO, it generally does not write a constructor. There is one by default, but when writing a constructor, pay attention to writing a constructor with a public null parameter

3: An identifier is required

4: Use non final classes (because proxies are used to delay the loading of entities)

5: Suppose you build an object: com bjpowernode. h4. hello. Usermodel has four attributes: UUID, userid, name and age

Create a table in the database

Let's build a table as TBL_ User, field: UUID, age

Configure XXX cfg. xml

1: The default name is hibernate cfg. xml

2: It is stored in the root directory of the current classes. It can be stored in the SRC root during development

3: There are four main configurations:

(1) Connection to DB

(2) Optional configuration

(3) Resource file registration

(4) L2 cache

4: When configuring, you can find a hibernate in the hibernate distribution package cfg. For example, you can use hibernate. XML under "\ project \ hibernate documentation \ QuickStart \ tutorials \ basic \ SRC \ test \ resources" cfg. XML as an example

5: Examples are as follows:

Configure XXX hbm. xml

1: It has the same name as the described class, such as usermodel hbm. xml

2: The storage location is stored in the same folder as the described class

3: There are four main configurations:

(1) Mapping of classes and tables

(2) Mapping of primary keys

(3) Class and mapping of fields in DB

(4) Mapping of relationships

4: When configuring, you can find an example in the hibernate distribution package. For example, you can use the customer. Under "\ project \ hibernate core \ SRC \ test \ Java \ ORG \ hibernate \ test \ CID" hbm. XML as an example

5: Examples are as follows:

Client files:

Test: just run the client file directly in elipse. After running, you will see the output in console: "hibernate: insert into tbl_user (userid, age, UUID) values (?,?,?)", Open the data table of the database, and you will see that a value has been added.

explain:

1:SessionFactory sf = new Configuration(). configure(). buildSessionFactory(); This sentence means to read hibernate cfg. XML, creating a session factory, is thread safe.

The default is "hibernate cfg. XML ", do not write it out, if the file name is not" hibernate cfg. XML ", you need to display the specified, as follows:

SessionFactory sf = new Configuration(). configure( “bjpowernode.cfg.xml” ). buildSessionFactory();

2: Session is the hibernate interface mainly used by the application. It is about the same as the connection + statement / Preparedstatement function of JDBC. It is thread unsafe

3: In Hibernate 4, the configuration class is no longer recommended. Instead, serviceregistrybuilder and metadatasources are used instead. The new writing method is roughly as follows:

This writing method has not been fully implemented yet and is not easy to use. Therefore, the previous method is still used in the official examples. Let's learn about it first.

4: The transaction used here is a hibernate transaction, which needs to be provided and cannot be removed.

Why is there a hibernate transaction? Take HelloWorld as an example:

summary

The above is a detailed explanation of the hibernate4 quick start example introduced by Xiaobian. I hope it will be helpful to you. If you have any questions, please leave me a message, and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>