Ali P8 tells you how to get started with the Java framework Hibernate

Source: Internet
Author: User

Hibernate is an open-source object-relational mapping framework that provides JDBC with a very lightweight object encapsulation that allows Java programmers to manipulate databases at will using object programming thinking. Hibernate can be applied to any JDBC application, both in Java client applications and in servlet/jsp Web applications, and most revolutionary of all, hibernate can replace CMP in the EE architecture of the EJB application. The task of achieving data persistence.

Flow chart:

Core interface

Hibernate has a total of 6 core interfaces: Session, Sessionfactory, Transaction, Query, criteria, and configuration. These 6 core interfaces will be used in any development. Through these interfaces, you can not only access persistent objects, but also enable transaction control. These 6 core interfaces are described separately below.

Session

The session interface is responsible for performing crud operations on persisted objects (the task of crud is to complete the communication with the database, including many common SQL statements. )。 However, it is important to note that the session object is non-thread-safe. At the same time, Hibernate's session differs from the HttpSession in JSP applications. When the term "session" is used, it is actually referring to the session in Hibernate, and later the HttpSession object is called the user session.

Sessionfactory

The Sessionfactory interface is responsible for initializing hibernate. It acts as a proxy for the data storage source and is responsible for creating session objects. The factory model is used here. It is important to note that Sessionfactory is not lightweight, because in general, a project typically requires only one sessionfactory, and when you need to manipulate multiple databases, you can specify a sessionfactory for each database.

Transaction

The Transaction interface is an optional API that allows you to opt out of using this interface instead of the underlying transaction code written by Hibernate's designers themselves. The Transaction interface is an abstraction of the actual transaction implementation, which includes JDBC transactions, usertransaction in JTA, and even CORBA transactions. This design is designed to enable developers to use a single transactional interface, allowing their projects to be easily ported between different environments and containers.

Query

The query interface makes it easy to make queries against databases and persistent objects, which can be expressed in two ways: the HQL language or the SQL statement for the local database. Queries are often used to bind query parameters, limit the number of query records, and ultimately perform query operations.

Criteria

The criteria interface is very similar to the query interface, allowing you to create and execute object-oriented, standardized queries. It is important to note that the criteria interface is also lightweight and cannot be used outside the session.

Configuration

The purpose of the configuration interface is to configure Hibernate and start it. During Hibernate startup, an instance of the configuration class first locates the location of the mapped document, reads the configuration, and then creates a Sessionfactory object. Although the configuration interface plays only a small role throughout the Hibernate project, it is the object that you encounter when you start hibernate.

Benefits of Hibernate

Hibernate handles mapped Java classes to use XML files, database tables, and no need to write any line of code.
Provides a simple API for storing and retrieving Java objects directly from a database.
If there is a change in the database or in any table then only the properties of the XML file need to be modified.
Abstract out unfamiliar SQL types and provide us with a familiar Java object to solve.
Hibernate does not require an application server to operate.
Manipulates complex associations of database objects.
Minimize access to the smart read policy database.
A simple query that provides data.

Hibernate configuration

To download the required package for hibernate first, I downloaded the hibernate-distribution-3.6.4.final, unzip the downloaded file, and the directory structure is as follows.

Then copy the package from Lib to the project.

You can also use MyEclipse to load hibernate frames directly.

Configuration file Hibernate.cfg.xml

<?xml version= ' 1.0 ' encoding= ' UTF-8 '?>
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<!--Generated by MyEclipse Hibernate Tools. -
<session-factory>
<property name= "dialect" >
Org.hibernate.dialect.MySQLDialect
</property>
<property name= "Connection.url" >
Jdbc:mysql://127.0.0.1:3306/java_web
</property>
<property name= "Connection.username" >root</property>
<property name= "Connection.password" >root</property>
<property name= "Connection.driver_class" >
Com.mysql.jdbc.Driver
</property>
<property name= "Myeclipse.connection.profile" >Test</property>
<property name= "Hbm2ddl.auto" >update</property>
<property name= "Hibernate.connection.autocommit" >true</property>
<property name= "Show_sql" >true</property>
<property name= "Defaultautocommit" >true</property>
<property name= "Maxactive" >10000</property>
<property name= "Maxidle" >50</property>
<property name= "Maxwait" >3000</property>
<property name= "InitialSize" >10</property>
<property name= "Minidle" >20</property>
<property name= "dialect" >
Org.hibernate.dialect.MySQLDialect
</property>
</session-factory>

Written in the end: Welcome message discussion, private messages "Java" or "schema material" have a surprise! Add attention, continuous UPDATE!!!

Ali P8 tells you how to get started with the Java framework Hibernate

Related Article

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.