Home Database Mysql Tutorial Hibernate大复习

Hibernate大复习

Jun 07, 2016 pm 04:10 PM
hibernate Architecture review

/* 1.Hibernate的体系结构 2.Hibernate API体系 *********************************************************************************************************************************************************************************************** Hib

/*
1.Hibernate的体系结构
2.Hibernate API体系
***********************************************************************************************************************************************************************************************


Hibernate API总结
名称 描述
Configuration类 负责配置和启动Hibernate,创建SessionFactory实例


SessionFactory接口 负责初始化Hibernate,创建Session实例,充当数据源代理,一个SessionFactory实例对应一个数据源,由于SessionFactory需要自己缓存
消耗的资源比较大,因此,当应用中只有一个数据源时,最好只创建一个SessionFactory对象实例,除非有多个数据源,才分别为每个数据源创建一个SessionFactory对象实例
Session接口 负责保存,更新,删除,加载和查询持久化对象,充当持久化管理器

Transaction接口 对底层的事务进行了封装,充当了事务管理器

Query接口,Criteria接口 执行数据库查询,充当Hibernate的查询器


***************************************************************************************************************************************************************************************************
Hibernate Web应用的开发步骤:
(1)创建数据源
(2)将Hibernate所需的JAR包复制到WEB-INF/lib下
(3)创建Hibernate配置文件
(4)利用Hibernate的第三方工具或Eclipse插件从数据库中创建出相应的实体对象其ORM映射文件
(5)创建Hibernate的SessionFactory类型
(6)通过SessionFactory对象创建Session实例
(7)通过创建Session实例进行持久化对象的管理
(8)通过创建Transaction实例进行事务管理
(9)通过创建Query或者Criteria实例实现数据库的查询


3.配置Hibernate
配置Hibernate主要就是创建Hibernate配置文件和SessionFactory类,Hibernate的配置文件可以是hibernate.properties或者
hibernate.cfg.xml(两者取其一),Hibernate.cfg.xml配置首选


































配置好hibernate.cfg.xml后,推荐保存在WEB-INF/classes下,接下来就可以创建SessionFactory了
package com.hephec.orm;


import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.cfg.Configuration;


public class MySessionFactory{
//定义一个静态字符串变量存放Hibernate的配置文件名
private static String CONFIG_FILE_LOCATION="/hibernate.cfg.xml";
//创建一个线程局部变量对象
private static final ThreadLocal threadLocal=newThreadLocal();
//创建一个静态的Configuration对象
private static final Configuration cfg=new Configuration();
//定义一个静态的SessionFactory对象
private static org.hibernate.SessionFactory sessionFactory;
//取得一个当前的Session对象
private static Session currentSession() throws HibernateException{
Session session=(Session)threadLocal.get();
if(session==null){
if(sessionFactory==null){
try{
//根据配置文件,配置Hibernate
cfg.config(CONFIG_FILE_LOCATION);
//通过Configuration对象创建SessionFactory对象
SessionFactory=cfg.buildSessionFactory();
}
catch(Exeption e){
System.out.println("系统错误创建SessionFactory对象出错!");
e.printStackTrace();
}
}
//通过SessionFactory对象创建Session对象
session=sessionFactory.openSession();
threadLocal.set(session);


}
return session;
}
//关闭一个Session对象
public static void closeSession()throws HibernateException(){
Session session=(Session)threadLoal.get();
threadLocal.set(null);
if(session!=null){
session.close();
}
}
//构造方法
public MySessionFactory(){
}
}


调用Hibernate API 进行持久化操作
package com.hephec.service;


import org.hibernate.*;
import java.util.*;
import com.hephec.orm;


public class SystemPart{
//用户验证
public boolean userCheck(String loginName,String loginPass) throws Exception{
//创建一个Session对象
Session session=MySessionFactory.currentSession();
//定义一个Transaction对象
Transaction tx=null;
try{
List result=null;
//创建一个Query查询对象
Query query=session.createQuery("select a from Admin as a where a.username=:loginName and a.loginPass=:loginPass");
//设置查询参数值
query.setString("loginName",loginName);
query.setString("loginPass",loginPass);
//创建一个Transaction对象
Transaction tx=session.beginTransaction();
//执行查询,得到查询结果
result=query.list();
tx.commit();
if(result.size()>0)return true;
else return false;
catch(Exception e){
//事务回滚
if(tx!=null){
tx.rollback();
}
System.out.println("系统错误!");
e.printStackTrace();
return false;
}finally{
//关闭Session对象
session.close();
}
}
}


4.Hibernate映射配置文件






5.Hibernate会话管理
*/




/*
Hibernate的映射机制
1.Hibernate基本映射数据类型
2.Hibernate的主键映射
3.Hibernate的实体映射
4.映射一对一关联关系
5.映射多对一的单向关联关系
6.映射一对多的双向关联关系
7.映射一对多双向自身关联关系
8.映射多对多单向关联关系
9.映射多对多双向关联关系
10.映射组成关系
11.映射继承关系
12.Hibernate映射集合
*/


/*
使用Session的beginTransction()方法
使用Session的close()方法
使用Session的connection()方法
使用Session的delete()方法
使用Session的get()方法
使用Session的load()方法
使用Session的update()方法
使用Session的saveOrUpdate()方法
使用Hibernate的isInitalized()与initialize()方法
持久化对象的级联操作
*/


/*
Hibernate的检索策略
1.立即检索
2.延迟检索
3.迫切左外连接检索
*/


/*
HQL查询方法
1.基本查询
2.条件查询
3.分页查询
4.连接查询
5.子查询
6.动态实例化查询结果
*/


/*
QBC查询方式
1.基本查询
2.QBE查询
3.分页查询
4.复合查询
5.离线查询
*/


/*
本地SQL查询
*/


/*
Hibernate批量操作
1.批量插入
2.批量更新
3.批量删除
*/


/*
Hibernate的事务管理
1.事务边界声明
2.并发控制
3.悲观锁
4.乐观锁
*/


/*
Hibernate缓存机制
1.Hibernate的缓存分类
2.Hibernate的缓存范围
3.Hibernate的缓存管理
4.Hibernate二级缓存的并发访问策略
5.Hibernate的二级缓存配置
*/


/*
Hibernate应用的性能优化
*/


/*
多数据源的应用
*/


/*
JDBC应用
*/


/*
Hibernate调用存储过程
*/


/*
XML数据持久化
*/










































































Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1269
29
C# Tutorial
1249
24
How to integrate Hibernate in SpringBoot project How to integrate Hibernate in SpringBoot project May 18, 2023 am 09:49 AM

Integrating Hibernate in SpringBoot Project Preface Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate persistence operations. In the SpringBoot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the SpringBoot project and provide corresponding examples. 1.Introduce dependenciesIntroduce the following dependencies in the pom.xml file: org.springframework.bootspring-boot-starter-data-jpam

Java Errors: Hibernate Errors, How to Handle and Avoid Java Errors: Hibernate Errors, How to Handle and Avoid Jun 25, 2023 am 09:09 AM

Java is an object-oriented programming language that is widely used in the field of software development. Hibernate is a popular Java persistence framework that provides a simple and efficient way to manage the persistence of Java objects. However, Hibernate errors are often encountered during the development process, and these errors may cause the program to terminate abnormally or become unstable. How to handle and avoid Hibernate errors has become a skill that Java developers must master. This article will introduce some common Hib

What are the differences between hibernate and mybatis What are the differences between hibernate and mybatis Jan 03, 2024 pm 03:35 PM

The differences between hibernate and mybatis: 1. Implementation method; 2. Performance; 3. Comparison of object management; 4. Caching mechanism. Detailed introduction: 1. Implementation method, Hibernate is a complete object/relational mapping solution that maps objects to database tables, while MyBatis requires developers to manually write SQL statements and ResultMap; 2. Performance, Hibernate is possible in terms of development speed Faster than MyBatis because Hibernate simplifies the DAO layer and so on.

What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate What is the mapping method of one-to-many and many-to-many relationships in Java Hibernate May 27, 2023 pm 05:06 PM

Hibernate's one-to-many and many-to-many Hibernate is an excellent ORM framework that simplifies data access between Java applications and relational databases. In Hibernate, we can use one-to-many and many-to-many relationships to handle complex data models. Hibernate's one-to-many In Hibernate, a one-to-many relationship means that one entity class corresponds to multiple other entity classes. For example, an order can correspond to multiple order items (OrderItem), and a user (User) can correspond to multiple orders (Order). To implement a one-to-many relationship in Hibernate, you need to define a collection attribute in the entity class to store

Introduction to Hibernate framework in Java language Introduction to Hibernate framework in Java language Jun 10, 2023 am 11:35 AM

Hibernate is an open source ORM framework that binds the data mapping between relational databases and Java programs to each other, making it easier for developers to access data in the database. Using the Hibernate framework can greatly reduce the work of writing SQL statements and improve the development efficiency and reusability of applications. Let's introduce the Hibernate framework from the following aspects. 1. Advantages of the Hibernate framework: object-relational mapping, hiding database access details, making development

How to configure the Hibernate environment in Java How to configure the Hibernate environment in Java Apr 26, 2023 am 11:55 AM

1. hibernate mapping configures the class tag, which is used to establish the relationship between the class and the table. name: class name, table: table name id tag, the corresponding relationship between the attribute being established and the primary key in the table property, and the establishment of ordinary attributes in the class. The corresponding relationship with the fields of the table (1) First of all, we must learn how to write the mapping configuration file. Everyone must know that the written mapping configuration file should be in the same package as the entity class, and the name should be class name.hbm.xml. So we need to create a Customer.hbm.xml file under the com.meimeixia.hibernate.demo01 package, but how should its constraints be written? Available in Hiberna

How does Hibernate second level cache work? How does Hibernate second level cache work? Sep 14, 2023 pm 07:45 PM

Caching helps reduce database network calls when executing queries. Level 1 cache and session linking. It is implemented implicitly. The first level cache exists until the session object exists. Once the session object is terminated/closed, there will be no cached objects. Second level cache works for multiple session objects. It is linked with the session factory. Second level cache objects are available to all sessions using a single session factory. These cache objects will be terminated when a specific session factory is closed. To implement the second level cache we need to add the following dependencies to use the second level cache. <!--https://mvnrepository.com/artifact/net.sf.ehcache/ehcache--><de

How to perform bulk insert update operations in Hibernate? How to perform bulk insert update operations in Hibernate? Aug 27, 2023 pm 11:17 PM

In this article, we will see how to perform bulk insert/update in Hibernate. Whenever we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries into the database table, then we have to make 10 network calls. Instead, we can optimize network calls by using batch processing. Batch processing allows us to execute a set of SQL statements in a single network call. To understand and implement this, let us define our entity − @EntitypublicclassParent{@Id@GeneratedValue(strategy=GenerationType.AUTO)

See all articles