Java Framework article---hibernate Getting Started

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.

SessionThe 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.sessionfactoryThe 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.TransactionThe 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.QueryThe 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.CriteriaThe 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.ConfigurationThe 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://hi Bernate.sourceforge.net/hibernate-configuration-3.0.dtd "><!--Generated by MyEclipse Hibernate Tools.  -<hibernate-configuration>    <session-factory>        < Propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ Property>        < Propertyname= "Connection.url">Jdbc:mysql://127.0.0.1:3306/java_web</ Property>        < Propertyname= "Connection.username">Root</ Property>        < Propertyname= "Connection.password">Root</ Property>        < Propertyname= "Connection.driver_class">Com.mysql.jdbc.Driver</ Property>        < Propertyname= "Myeclipse.connection.profile">Test</ Property>        < Propertyname= "Hbm2ddl.auto">Update</ Property>        < Propertyname= "Hibernate.connection.autocommit">True</ Property>        < Propertyname= "Show_sql">True</ Property>        < Propertyname= "Defaultautocommit">True</ Property>        < Propertyname= "Maxactive">10000</ Property>        < Propertyname= "Maxidle">50</ Property>        < Propertyname= "Maxwait">3000</ Property>        < Propertyname= "InitialSize">10</ Property>        < Propertyname= "Minidle">20</ Property>        < Propertyname= "dialect">Org.hibernate.dialect.MySQLDialect</ Property>    </session-factory></hibernate-configuration>

Java Framework article---hibernate Getting Started

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.