Hibernate environment construction and demo sharing

ORM concept

ORM is object / relationship mapping. ORM is a specification that completes the mapping between object-oriented programming language and relational database. JPA in J2EE is an ORM specification.

There are many ORM frameworks, such as JPA, hibernate, ibatis, etc.

Introduction to hibernate

Hibernate is not only a product of JBoss, but also a product of rethat Organization (JBoss joined rethat). It is a very popular ORM framework at present.

The important concept in Hibernate is po (persistent object). Hibernate adopts a low intrusion design. The Po here is completely an ordinary Java class (POJO), and its database operation function is completely implemented by hibernate. POJO does not need to implement any interface or inherit any superclass.

Hibernate environment construction (eclipse environment)

1. Download framework

Hibernate framework, downloaded from the official website http://www.hibernate.org/downloads

The latest version is 5.2 2. For compatibility and stability, I downloaded 4.3 Version 11, hibernate-release-4.3 11.Final. Zip. After decompression, you can see the main directory as follows,

-Project, there are many demo projects in this directory

-Various documents and tutorials are placed under the documentation. The most important is the hibernate API, namely JavaDocs

-There are many secondary directories under lib, which contain various jar packages. Hibernate is modular. Required is the basic jar package of Hibernate framework, and other directories are some extension packages. For example, lib \ optional \ c3p0 contains the jar package of database connection pool.

In addition, you also need to download the log framework package slf4j, which hibernate will use to output logs during execution.

I downloaded 1.6 Version 1, which can be found in the data warehouse on the official website http://www.slf4j.org/dist/

2. Import various jar packages

First create a new project in eclipse, and then create a new user library, for example, hibernate-4-3-11. Be careful not to check system library, otherwise Java will always be reported when reading hibernate configuration files later Lang.nullpointerexception exception.

Import the following jar packages

-All jar packages (10) under lib \ require under hibernate, which are the basic jar packages of the framework

-All jar packages of Lib \ optional \ c3p0 under hibernate, which are database connection pool jar packages, provide data sources for hibernate framework

-Slf4j-api-1.6 under slf4 framework 1. Jar (this is the API) and slf4j-nop-1.6.1. Jar (this is the concrete implementation)

I put all jar packages in one directory to facilitate future migration. All jar packages are as follows,

Add the above 15 jars to the user library.

3. Create an entity class

The new class will be used to correspond to a table in the database. It is just a common class (POJO). We put it in the Src / Hib path. Later, hibernate will create a data table according to the configuration file

4. Create a table mapping file

Create an XML file news. XML in the same path as the news class hbm. XML, this file is associated with news Java correspondence, called mapping file, is a database that hibernate will operate based on this file.

Through some plug-ins, you can use the entity class news Java automatically creates news hbm. XML, but I'd better finish and create it first.

News. hbm. The syntax of XML has been described in detail in the manual provided by hibernate (hibernate-release-4.3.11. Final / documentation / manual / en US / html_single / index. HTML) 1.1.3. The mapping file,

Each persistent entity class (such as news. Java above) needs to have a mapping to the data table. The < class > element here is a mapping

The primary key of the table is represented by the < ID > element, and other fields are represented by the < property > element. Each field element can be added with name, colume and type attributes to represent the field name and type respectively

5. Create hibernate master configuration file

The default name of the hibernate configuration file is hibernate cfg. XML, we create this file and put it in the SRC root directory. The contents of the file are as follows,

The above key points are explained as follows:

Database connection string: if you want to specify a character set, add "string" after the URL? Useunicode = true & characterencoding = UTF-8, but because the URL is to be placed in the XML file, the & symbol needs to be escaped to "&"

I also use < property name = "connection. Charset" > utf8 < / property > this method is compatible with Chinese, but it doesn't seem to work

Database Dialect: I use MySQL database. At first, the database dialect configuration I use is org hibernate. dialect. Mysqlinnodbdialect, but it was found that the table could not be created automatically through hibernate. Later, it was found that it was changed to org hibernate. dialect. Just mysqldialect.

All database dialect configurations can be found in the manual, hibernate-release-4.3 11.Final/documentation/manual/en-US/html_ single/index. HTML #tutorial-firstapp-mapping 3.4 1. sql Dialects

Whether to automatically create tables according to entity classes and mapping files: < property name = "hbm2ddl. Auto" > Update < / property >

Print SQL to console: < property name = "show_sql" > true < / property >

6. Create test class

Newsmanager is also placed in the Hib path. In this class, hibernate is initialized to perform database operations

Note the configuration () above The configure () method loads the hibernate configuration file hibernate. Exe from the default path Src cfg. xml。

Start the MySQL database (ensure that the database name in the above configuration file exists), and then execute the newsmanager class in eclipse. The results are as follows. You can see that the SQL statement is promised at the end,

At the same time, we checked the MySQL database and found that in think_ There is an additional table news under the blog library_ Table. At the same time, we insert a piece of data in the table,

At this point, our hibernate environment is successfully configured and a basic demo is successfully executed.

The general steps of Hibernate can be summarized from the test class,

-Load the hibernate configuration file (hibernate.cfg.xml, which is loaded from the SRC directory by default) to obtain the configuration instance of hibernate

-Create a session factory through an instance of configuration

-Open a session through the session factory

-Start a transaction through session

-Set or get the entity class

-Save the operation results of the entity class into the session

-Commit transaction

-Close session and session factory resources

The above steps are completely object-oriented programming and do not directly operate the database, but complete the database operation through hibernate, which is the basic principle of hibernate.

This hibernate environment construction and demo sharing is all the content shared by Xiaobian. I hope it can give you a reference and support programming tips.

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
分享
二维码
< <上一篇
下一篇>>