Hibernate learning 0. Hibernate getting started, photoshop7.0 getting started

Source: Internet
Author: User

Hibernate learning 0. Hibernate getting started, photoshop7.0 getting started
What is Hibernate?

Object/Relational Database ing tool for java environment.

1. Open-source persistent layer framework.

2. The ORM (Object/Relational Mapping) ing tool establishes the ing between the Object-Oriented Domain Model and the Relational data model.

3. Middleware connecting java applications and databases.

4. encapsulate JDBC to ensure the persistence of java objects.

5. In the layered structure, the persistence layer encapsulates access details to the database, so that the business logic layer is more focused on implementing business logic.

Functions of Hibernate

Hibernate is a bridge between Java applications and relational databases. It is responsible for ing between Java objects and relational data. Hibernate encapsulates operations to access the database through JDBC, and provides object-oriented data access APIs for upstream applications. ORM Introduction

Object-relational ing (ORM ).

A single component is responsible for the persistence of all object domain objects and encapsulating data access details.

Object-link ing Concept

Object-oriented concepts

Relationship-oriented concept

Class

Table

Object

Table row (record)

Attribute

Table columns (fields)

Usage of ORM middleware

Metadata is used to describe the details of Object-link ing. Metadata is usually in xml format and stored in a dedicated object-link ing file. As long as the ing between persistence classes and tables is configured, orm middleware can persistently store domain objects in the database by referring to the information of the ing file at runtime.

public void deleteCustomer(Customer c){      Session session = getSession();      session.delete(c);}

Perform the following steps:

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

2. Refer to the ing file to obtain the information about the table corresponding to the Customer class, the class associated with the Customer class, and the corresponding table information.

3. generate an SQL statement based on the preceding information.

4. Call the jdbc api to execute this statement.

To use the Hibernate software package, you need to obtain the Hibernate jar package and some corresponding third-party packages. When using Hibernate, you need to use some third-party jar packages under the Hibernate. jar and lib directories.
  • Hibernate3.jar: contains the basic framework and core API class library of Hibernate3. It is a required jar package.
  • Cglib-2.1.2.jar: CGLIB library, Hibernate uses it to realize dynamic generation of PO bytecode, it is a very core library, is a must use jar package.
  • Dom4j-1.6.1.jar: dom4j is a Java xml api, similar to jdom, used to read and write XML files.
  • Commons-collections.jar: One of the Apache Commons packages contains some collection classes developed by Apache, which are more powerful than java. util. Required jar package.
  • Commons-logging.jar: One of the Apache Commons packages contains the daily-to-function, a required jar package.
  • Ant-1.6.5.jar: Ant compilation tool jar package, used to compile 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 C3PO connection pool, if you are prepared to use this connection pool, you need this jar package.
  • Connector. jar: JCA (Java Cryptography Architecture, Java encryption Architecture, framework used for accessing and developing encryption functions on the java platform) specification. If Hibernate is configured as Connector on the App Server, this jar is required. Generally, the App Server carries this package.
  • Jaas. jar: JAAS is used for permission verification and is included in JDK1.4. So it is actually a redundant package.
  • Jdbc2_0-stdext.jar: JDBC2.0 extension package, in general, the database connection pool will use it, but the App Server will carry it, so it is redundant.
  • Jta. jar: JTA (java Transaction Processing Mechanism) specification. It is required when Hibernate uses JTA, but it is also redundant because it is used by the App Server.
  • Junit-3.8.1.jar: Junit package, when running Hibernate comes with the test code needs, otherwise it does not need.
  • Xerces-2.6.2.jar and xml-apis.jar: Xerces is the XML parser, and xml-apis is actually JAXP. It is also a redundant package.
Hibernate API Introduction

1. Provides database access operations (session, transaction, query)

2. Configure the hibernate interface (configuratioin)

3. Callback interface (Interceptor, Lifecycle, Validatable)

4. extended functional interfaces (UserType, CompositeUserType, IdentifierGenerator)

Hibernate implements lightweight encapsulation of JDBC. The so-called lightweight means that Hibernate does not fully encapsulate JDBC. java applications can access the database through the HibernateAPI and directly access the database through the jdbc api.

Hibernate core interface

1. Configuration

Configure hibernate, start hibernate at the root, and create the sessionFactory object.

2. sessionFactory

Creates a Session object. You can create a SessionFactory object through the Configuration object.

The SessionFactory object stores the current database configuration information, all ing relationships, and predefined SQL statements. SessionFactory is also responsible for maintaining the level-2 cache of Hibernate.

The creation of SessionFactory objects has a large overhead, and SessionFactory objects adopt a thread-safe design method. Therefore, in practice, SessionFactory objects can be shared as much as possible. In most cases, an application can share a SessionFactory instance for a database.

Configuration config = new Configuration (). configure ();

SessionFactory factory = config. buildSessionFactory ();

3. Session

The most widely used persistence manager provides persistence-related operations. Add, delete, modify, and query. It is not thread-safe. Therefore, a Session object can only be used by one thread. Avoid sharing multiple threads. Lightweight, creating and destroying does not need to consume too much resources. The Session contains a cache called a level-1 cache. Stores the objects loaded by the current work unit.

Configuration config = new Configuration (). configure ();

SessionFactory factory = config. buildSessionFactory ();

Session session = factory. openSession ();

4. Transaction hibernate Database Transaction Interface

Abstract The application code from the underlying transaction implementation-this may be a JDBC transaction, a JTA user transaction, or even a Common Object Request proxy (CORBA) -- allows applications to control transaction boundaries through a set of consistent APIs.

Transaction (default: autoCommit = false)

Transaction tx = session. beginTransaction ();

5. Query and Criteria Interfaces

These are all query Interfaces. The query instance encapsulates the HQL query statement. hql is object-oriented and references the class name and class attribute name, rather than the table name and field name. The Criteria interface completely encapsulates query statements in the string format. It is more object-oriented than the query interface and is good at executing dynamic queries. The find method of the sessoin interface also provides the data query function, but it is just a shortcut for executing some simple hql query statements, far from being powerful.

Callback Interface

1. Lifecycle and Validatable interfaces: lifecycle interfaces enable persistent class responses to be loaded, saved, and deleted. The Validatable interface verifies the data before the persistence class interface is saved. This method forces the persistence class to implement specific interfaces, so that the hibernate API can penetrate into the persistence class, which affects portability and is not recommended.

2. Interceptor: it does not need to be implemented by persistence classes. You can define a class that specifically implements this interface to respond to events that are loaded, saved, updated, or deleted by persistence class instances.

Hibernate ing type interface

1. PrivateType class: ing Basic java data types, including ByteType, ShortType, IntegerType, LongType, FloatType, DoubleType, CharactorType, and BooleanType.

2. DateType: ing java date type.

3. BinaryType: ing Byte [] type.

Query. setParameter ("name", "name", Hibernate. STRING)

Available Interfaces

Most of the functions provided by hibernate are configurable, allowing users to select appropriate built-in policies. You can configure the following database dialects:

Hibernate. dialect = net. sf. hibernate. dialect. MySQLDialect

Hibernate. dialect = net. sf. hibernate. dialect. OracleDialect

Hibernate. dialect = net. sf. hibernate. dialect. SybaseDialect

If the built-in policies of hibernate cannot meet your requirements, you can customize the implementation interface or extend a specific class. Hibernate extensions include:

Primary Key Generation Policy: IdentifierGenerator

SQL Dialect: Dialect abstract class

Cache Mechanism: cache and cacheprovider Interfaces

Jdbc Connection Manager: connectionprovider

Hibernate running process

Hibernate runs as follows:

1. The application first calls the Configuration class, which reads information in the Hibernate Configuration file and the ing file,

2. Use the information to generate a SessionFactory object,

3. Generate a Session object from the SessionFactory object,

4. Generate a Transaction object with the Session object;

A. You can use get (), load (), save (), update (), delete (), and saveOrUpdate () of the Session object () loading, saving, updating, deleting, and other operations on PO;

B. When querying, you can use the Session object to generate a Query object and then use the Query object to perform the Query operation. If no exception exists, the Transaction object submits these operations to the database.

Summary

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.