Hibernate learning 0.Hibernate Getting Started

Source: Internet
Author: User

What is Hibernate?

Object/relational database mapping tool for the Java environment.

1. Open source persistence layer framework.

The 2.ORM (object/relational Mapping) Mapping tool establishes the mapping between the object-oriented domain model and the relational data model.

3. Middleware that connects Java applications and databases.

4. Encapsulates JDBC, which is responsible for the persistence of Java objects.

5. The persistence layer in the hierarchy, encapsulating the access details of the database, and making the business logic layer more focused on implementing the business logic

Hibernate effect

Hibernate is a bridge between Java applications and relational databases, which are responsible for mapping between Java objects and relational data. Hibernate internally encapsulates the operation of accessing the database through JDBC, providing an object-oriented data access API to upper-level applications. ORM Introduction

Object-Relational mapping (ORM, that is, Object-relation Mapping).

Refers to the persistence of all entity domain objects in a single component, encapsulating data access details.

Concept of object-relational mapping

Object-oriented concepts

Relationship-oriented concept

Class

Table

Object

Rows of Tables (records)

Property

Table columns (fields)

How to use ORM Middleware

Metadata is used to describe object-relational mapping details, and metadata is typically in XML format and stored in a dedicated object-relational mapping file. As long as the mappings between persisted classes and tables are configured, ORM Middleware can persist the domain objects to the database by referencing the information of the mapping file at runtime.

 Public void DeleteCustomer (Customer c) {      = getsession ();      Session.delete (c);}

The following steps are performed:

1. Use the reflection mechanism to obtain the Customer.class class of the Customer object.

2. Refer to the mapping file to get the information of the table corresponding to the customer class, and the class associated with the customer class and the corresponding table information.

3. Generate SQL statements based on the above information.

4. Call the JDBC API to execute the statement.

Hibernate package to use Hibernate, you need to get Hibernate's jar package and some corresponding third-party packages. When using hibernate, you need to use some third-party jar packages in the Hibernate.jar and Lib directories.
  • Hibernate3.jar: Contains Hibernate3 's underlying framework and core API class libraries, which are the jar packages that must be used.
  • Cglib-2.1.2.jar:cglib Library, Hibernate uses it to achieve the dynamic generation of PO bytecode, it is a very core library, it is necessary to use the jar package.
  • DOM4J-1.6.1.JAR:DOM4J is a Java XML API, similar to Jdom, used to read and write XML files.
  • One of the Commons-collections.jar:apache Commons packages contains some Apache-developed collection classes that are more powerful than java.util.*. The jar package that must be used.
  • One of the Commons-logging.jar:apache Commons packages, containing the day-to-function, must use the jar package.
  • The jar package for the Ant-1.6.5.jar:ant compilation tool, which compiles the hibernate source code. It is an optional package.
  • C3PO-0.9.0.JAR:C3PO is a database connection pool, hibernate can be configured to use the C3PO connection pool, and if you are ready to use this connection pool, you will need this jar package.
  • Connector.jar:JCA (Java Cryptography Architecture,java Encryption architecture, framework for accessing and developing cryptographic functions in the Java Platform) specification, if the app This jar is required to configure Hibernate as connector on the server. General app Server will bring this package.
  • The Jaas.jar:JAAS is used for authorization verification and is already included in the JDK1.4. So it's actually an extra bag.
  • jdbc2_0-stdext.jar:jdbc2.0 expansion pack, generally the database connection pool will use it, but the app server will take it, so it is redundant.
  • Jta.jar:JTA (the Java Transaction Mechanism) specification is required when Hibernate uses JTA, but the app server will take it, so it's redundant.
  • The Junit-3.8.1.jar:junit package is required when running Hibernate's own test code, or not.
  • Xerces-2.6.2.jar and Xml-apis.jar:xerces are XML parsers, Xml-apis is actually JAXP. It is also an extra bag.
Hibernate API Introduction

1. Providing access to database operations (Session,transaction,query)

2. Configure Hibernate interface (Configuratioin)

3. Callback Interface (interceptor,lifecycle,validatable)

4. Function interface for expansion (Usertype,compositeusertype, Identifiergenerator)

Hibernate does a lightweight encapsulation of JDBC. The so-called lightweight means that Hibernate is not fully encapsulated Jdbc,java applications can access the database through HIBERNATEAPI, and access the database directly through the JDBC API.

Hibernate Core Interface

1.Configuration

Configure hibernate, Root starts hibernate, and create Sessionfactory objects.

2. Sessionfactory

Responsible for creating the Session object, you can create the Sessionfactory object from the configuration object.

The Sessionfactory object holds the current database configuration information and all mapping relationships, as well as predefined SQL statements. At the same time, Sessionfactory is responsible for maintaining Hibernate's Level two cache.

Sessionfactory objects are expensive to create, and Sessionfactory objects are designed in a thread-safe way, so in practice Sessionfactory objects can be shared as much as possible, and in most cases A single sessionfactory instance can be shared in one application for a database

Configuration config = new configuration (). Configure ();

Sessionfactory factory = Config.buildsessionfactory ();

3.Session

The most widely used, also known as the Persistence Manager, provides and persists related operations. increase, delete, change, check and so on. is not thread-safe, so a session object can only be used by a single thread. Avoid multiple thread sharing. Lightweight, creation and destruction do not need to consume too much resources. There is a cache in the session, called the first level cache. The object that holds the current unit of work loaded.

Configuration config = new configuration (). Configure ();

Sessionfactory factory = Config.buildsessionfactory ();

Session session = Factory.opensession ();

4.Transaction Hibernate database Transaction interface

The application code is abstracted from the underlying transaction implementation-this could be a JDBC transaction, a JTA user transaction, or even a common object request broker structure (CORBA)-allowing the application to control the transaction boundaries through a consistent set of APIs.

Call Transaction (default: Autocommit=false) that must be displayed when working with Hibernate (add, delete, change)

Transaction tx = Session.begintransaction ();

5.Query and Criteria Interface

is the query interface, the query instance wraps the HQL query statement, HQL is object-oriented, he refers to the class name and the class's property name, not the table name and the field name. The criteria interface completely encapsulates a query statement based on a string, which is more object-oriented than the query interface and is adept at executing dynamic queries. The Find method of the Sessoin interface also has the data query function, but he just executes some simple HQL query statement shortcut, far without the query interface powerful.

Callback Interface

1.Lifecycle and Validatable interfaces: The lifecycle interface enables persistent class responses to be loaded, saved, and deleted events. The Validatable interface enables data validation before the interface of the persisted class is saved. This approach forces the persistence class to implement a specific interface, so that the Hibernate API penetrates into the persistence class, which affects portability and is not recommended.

2.Interceptor: It does not have to be implemented by a persistence class. You can define a class that specifically implements the interface and is responsible for responding to events that are loaded, saved, updated, or deleted by instances of the persisted class.

Hibernate mapping Type interface

1.PrivateType class: Maps Java basic data types, including Bytetype, Shorttype, Integertype, Longtype, Floattype, Doubletype, Charactortype, Booleantype.

2.DateType: Maps the Java date type.

3.BinaryType: Map byte[] type.

Query.setparameter ("name", "name", hibernate.string)

Extensible interface

Hibernate provides most of the functionality that is configurable, allowing the user to select the appropriate built-in policy. If you can configure the following database dialect:

Hibernate.dialect=net.sf.hibernate.dialect.mysqldialect

Hibernate.dialect=net.sf.hibernate.dialect.oracledialect

Hibernate.dialect=net.sf.hibernate.dialect.sybasedialect

Allows users to customize implementation interfaces or extend specific classes if Hibernate's built-in policies do not meet the requirements. The extension points for hibernate include:

Primary key generation policy: Identifiergenerator

SQL dialect: Dialect abstract class

Caching mechanism: Cache and Cacheprovider interfaces

JDBC Connection Manager: ConnectionProvider, etc.

Hibernate run Process

Hibernate is run as follows:

1, the application first calls the configuration class, which reads the hibernate configuration file and the information in the mapping file,

2. Use this information to generate a Sessionfactory object,

3. Then generate a Session object from the Sessionfactory object,

4, using the Session object to generate transaction objects;

A, through the Session object get (), load (), save (), update (), delete () and saveorupdate () and other methods to load, save, update, delete, and other operations;

B, in the case of the query, you can generate a query object through the Session object, and then use the Queries object to perform the querying operation; if there is no exception, the transaction object submits these operations to the database.

Summary

Hibernate learning 0.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.