Home » Java » Hibernate core API detailed explanation

Hibernate core API detailed explanation

Configuration
Configuration CFG = new, Configuration ();
Cfg.configure ();
From 1. to SRC, find the name hibernate.cfg.xml configuration file, create the object, and put the configuration file inside the object (loading the configuration file)

SessionFactory (emphasis)

creates the sessionFactory object
using the configuration object
- automatically create a table when you want to configure the project

Update


consumes resources especially when creating sessionFactory,


In the hibernate operation, it is recommended that a project generally create a sessionFactory object

implementation (creating a sessionFactory object)


Write a tool class, write a static code block, and implement

Static code blocks are executed only once when loaded in a class
 package com.jeff.util;
Import org.hibernate.SessionFactory;
Import org.hibernate.cfg.Configuration;
Public, class, HibernateUtil {
Private, final, static, Configuration, cfg;
Private, final, static, SessionFactory, sessionFactory;
Static code block / / sessionFactory object creation
Static{
CFG = new, Configuration ();
Cfg.configure ();
SessionFactory = cfg.buildSessionFactory ();
}
Public, static, SessionFactory, getSessionFactory () {
Return sessionFactory;
}
}

Session

is similar to connection

in JDBC

calls session, inside different methods to achieve CRUD operations,


Add save ()
Update update ()
Delete delete ()
According to the ID query, the get method
The

session object is a single thread object


Session objects cannot be shared
Transaction

transaction object


commits and rolls back

Commit ();

Rollback ();


concept

Atomicity, consistency, isolation, persistence,


Object for query implementation

Query object


uses this object without writing the SQL statement, but writes the HQL statement


The difference between

SQL and HQL


SQL
HQL
SQL operations are tables and fields
HQL operations entity classes and properties

HQL query


queries all records


from entity class name

The use of

Query objects


Creating Query objects
Write the HQL statement in the method
Call the Query method to get the result set

//1. creates Query objects
Write a HQL statement / method
Query query = session.createQuery ("from User");
//2. calls the query method to get the result
List userList = query.list ();
For (User user: userList) {
System.out.println (user.toString ());
}
*
- running results
Hibernate:
Select
User0_.uid as uid1_0_,
User0_.username as username2_0_,
User0_.password as password3_0_,
User0_.address as address4_0_
From
T_user user0_
User, [uid=1, username=Jeff, password=123456, address=, Guangdong, Maoming.]
User, [uid=2, username=, Li Bai, password=123456, address=, Guangdong, Guangzhou.]
User, [uid=3, username=, pony, password=123456, address=China]
* /

Criteria (standard) object


There is no need to use statements

implementation process


creates Criteria object


Call the corresponding query method of the object
//1. creates Criteria objects
Criteria criteria = session.createCriteria (User.class);
//2. calls object methods to get results
List userList = criteria.list ();

SQLQuery object


Implementing raw SQL queries
//1. creates SQLQuery objects
SQLQuery sqlQuery = session.createSQLQuery ("select * from, t_user");
//2. calls object methods to get results
List list = sqlQuery.list ();
For (Object[] obj: list) {
/ / convert an array to a string
String arrayToStr = Arrays.toString (obj);
System.out.println (arrayToStr);
}
*
-- running results --
Hibernate:
Select
*
From
T_user
[1, Jeff, 123456, Guangdong, Maoming]
[2, Li Bai, 123456, Guangdong, Guangzhou.]
[3, pony, 123456, China]
* /
- the query method of the SQLQuery object returns an array of object objects when it is not processed

The method of implementing the returned result set is an object
//1. creates SQLQuery objects
SQLQuery sqlQuery = session.createSQLQuery ("select * from, t_user");
Method returns the object / /
SqlQuery.addEntity (User.class);
//2. calls object methods to get results
List list = sqlQuery.list ();

Latest