Hibernate Object Persistence framework and hibernate object framework

Source: Internet
Author: User
Tags driver manager

Hibernate Object Persistence framework and hibernate object framework
JDBC :( Java Data Base Connectivity) java Database Connection

The java. SQL package provides JDBC APIs to write program code for accessing the database. common interfaces and classes include the following:

  • DriverManager: Driver manager, responsible for creating database connections
  • Connection: indicates a database Connection.
  • Statement: executes SQL statements.
  • PreparedStatement: executes SQL statements with predefined functions.
  • ResultSet: indicates the query result set of the SQL query statement.


Hibernate (Java object persistence technology): Hibernate is an open-source object relationship ing framework that encapsulates JDBC with lightweight objects, this allows Java programmers to manipulate the database using the object programming thinking as they wish. Hibernate can be used in any situation where JDBC is used. It can be used in Java client programs or Servlet/JSP Web applications. The most revolutionary is that, hibernate can replace CMP in the J2EE architecture of application EJB to fulfill the task of data persistence.

Hibernate has six core interfaces: Session, SessionFactory, Transaction, Query, Criteria, and Configuration.
Session interface:Executes the CRUD operation of the Persistent Object (the CRUD task is to complete the communication with the database, including many common SQL statements .). However, it should be noted that the Session object is non-thread-safe. At the same time, the Hibernate session is different from the HttpSession In the JSP application. Here, when session is used, it actually refers to the session in Hibernate, and later the HttpSession object will be called the user session.

SessionFactory interface: initializes Hibernate. It acts as a proxy for the data storage source and is responsible for creating Session objects. The factory model is used here. Note that SessionFactory is not lightweight, because generally, only one SessionFactory is required for a project. When you need to operate multiple databases, you can specify a SessionFactory for each database.

Transaction interface:Is an optional API, you can choose not to use this interface, instead is the underlying transaction processing code written by the Hibernate designer. The Transaction interface is an abstraction of the actual Transaction implementation. These implementations include JDBC transactions, UserTransaction in JTA, and even CORBA transactions. This design allows developers to use a unified transaction operation interface so that their projects can be easily moved between different environments and containers.

Query interface:This allows you to easily query databases and persistent objects. It can be expressed in two ways: HQL or SQL statements of the local database. Query is often used to bind Query parameters, limit the number of Query records, and finally perform Query operations.

Criteria interface:Similar to the Query interface, you can create and execute object-oriented standard queries. It is worth noting that the Query interface is also lightweight and cannot be used outside the Session.

Configuration interface:It is used to configure and start Hibernate. During Hibernate startup, the Configuration instance first locates the ing document, reads the configurations, and creates a SessionFactory object. Although the Configuration interface only plays a small role in the entire Hibernate project, it is every object encountered when hibernate is started.


How does hibernate achieve persistence?

Have you ever used hibernate?

Hibernate is An ORM (object relation mapping, object relationship ing) framework. The so-called object relationship ing is to save JAVA objects to relational databases.

What hibernate needs to do is to project objects to relational databases and implement persistence. Specific implementation:
Creates a ing from an object to a relational database.
Example: create a User ing from the User class to the database
A simple class User:
Public class User {
Private int id;
Private String name;
}

Database Table User corresponding to the USER class:
Column-name type rule
Id number P
Name varchar2 R

When performing persistence, you need to save the values of the two attributes of the object to the USER in the data inventory. If JDBC is used, you need to write SQL by yourself, and get the values of the two attributes of the object as parameters for persistence. The SQL statement may be as follows:
Insert into USER (name) value (?);

When hibernate is used, there is no ing relationship between objects and tables at first, and the configuration file is used for implementation:
User. hbm. xml:
...
<Class name = "user" table = "USER"> <! -- Save the Class Object to the USER table -->
<Id name = "id" column = "id">
<Generator class = "native"/> <! -- There are many policies to generate IDS. Select Local Automatic Generation here. -->
</Id>
<Property name = "name" column = "name">
</Class>
...

Then, configure the connection to the database through hibernate. cfg. xml and read the User. hbm. xml file to establish a ing relationship.

When you need persistence objects, you need to use the hibernate session object:
Session. save (new User ());
Have you seen it? We only need to operate on the object. That is to say, the programmer does not need to see the database, and only needs to directly operate on the object.

How does hibernate achieve persistence?
A:
1. hibernate is An ORM (object relation mapping, object relationship ing) framework. The so-called object relationship ing is to save JAVA objects to relational databases, that is, the persistence framework.

2. for persistence of hibernate, you must first establish the ing between objects and relational databases.

3. hibernate persists (saves) the object to the data inventory according to the defined ing rules. This achieves Object persistence .... Remaining full text>

What are the three persistence states of hibernate objects?

Instantaneous State: Simply put, you create an object in the program and it is not associated with the session.
Persistent state: the object is associated with the session, and the object is under hibernate framework management.
Free State: In the persistent state, the object and session are lost, such as session. close () or session. flush ()
But the data has been stored in the database.

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.