【Hibernate】Re02 官网介绍
介绍:
创始人:Gavin King。EJB3.0专家,JBoss核心成员之一,《Hibernate In Action》作者
Hibernate是ORM的解决方案。
优点:
1、功能强大,减少代码量,提高持久化开发速度,降低维护成本
2、面向对象特点强调,组合,继承,多态
3、可移植性,即不依赖数据库的提供者,MySQL、Oracle、SqlServer只需要更换接口和配置文件即可运行
4、开源免费,允许改写源码,可扩展性好
缺点:
1、不适合大量使用存储过程的应用
2、不适合大规模批量的插入,修改,删除,内存消耗大
和Mybatis的对比:
Mybatis面向SQL-Mapping实现ORM
Hibernate面向对象实现ORM,对对象完善更多 Mybatis使用SQL语句,和数据库有关,移植性不如Hibernate,移植工作量大
Hibernate不关注SQL语句和结果映射,HQL与数据库无关 Mybatis直接使用SQL语句,灵活性相比Hibernate更好
Hibernate不适用关系模型设计不合理,不规范的系统,缓存也不如Mybatis
前言:
同时使用面向对象软件和关系数据库可能会很麻烦而且耗时。
由于对象和关系数据库中数据的表示方式不匹配,开发成本显著提高。
Hibernate是一个面向Java环境的对象/关系映射解决方案。
对象/关系映射是指将数据从对象模型表示映射到关系数据模型表示(反之亦然)的技术。
Hibernate不仅负责从Java类到数据库表(以及从Java数据类型到SQL数据类型)的映射,而且还提供数据查询和检索工具。
它可以显著减少开发时间,否则将花费在SQL和JDBC中的手动数据处理上。
Hibernate的设计目标是通过消除使用SQL和JDBC手工处理数据的需求,使开发人员从95%的与数据持久性相关的编程任务中解脱出来。
然而,与许多其他持久性解决方案不同,Hibernate并没有向您隐藏SQL的强大功能,并保证您在关系技术和知识方面的投资始终有效。
对于只使用存储过程实现数据库中的业务逻辑的以数据为中心的应用程序,Hibernate可能不是最好的解决方案,它对于基于Java的中间层中的面向对象的域模型和业务逻辑最有用。
然而,Hibernate当然可以帮助您删除或封装特定于供应商的SQL代码,并有助于完成从表格表示到对象图的结果集转换这一常见任务。
Preface
Working with both Object-Oriented software and Relational Databases can be cumbersome and time-consuming.
Development costs are significantly higher due to a paradigm mismatch between how data is represented in objects versus relational databases.
Hibernate is an Object/Relational Mapping solution for Java environments.
The term Object/Relational Mapping refers to the technique of mapping data from an object model representation to a relational data model representation (and vice versa). Hibernate not only takes care of the mapping from Java classes to database tables (and from Java data types to SQL data types), but also provides data query and retrieval facilities.
It can significantly reduce development time otherwise spent with manual data handling in SQL and JDBC.
Hibernate’s design goal is to relieve the developer from 95% of common data persistence-related programming tasks by eliminating the need for manual, hand-crafted data processing using SQL and JDBC.
However, unlike many other persistence solutions, Hibernate does not hide the power of SQL from you and guarantees that your investment in relational technology and knowledge is as valid as always. Hibernate may not be the best solution for data-centric applications that only use stored-procedures to implement the business logic in the database,
it is most useful with object-oriented domain models and business logic in the Java-based middle-tier.
However, Hibernate can certainly help you to remove or encapsulate vendor-specific SQL code and will help with the common task of result set translation from a tabular representation to a graph of objects.
系统要求:
Hibernate5.2及更高版本至少需要Java1.8和JDBC4.2。
Hibernate5.1及更早版本至少需要Java1.6和JDBC4.0。
从源代码构建Hibernate5.1或更高版本时,由于JDK1.6编译器中存在错误,您需要Java1.7。
原文:
System Requirements
Hibernate 5.2 and later versions require at least Java 1.8 and JDBC 4.2.
Hibernate 5.1 and older versions require at least Java 1.6 and JDBC 4.0.
When building Hibernate 5.1 or older from sources, you need Java 1.7 due to a bug in the JDK 1.6 compiler.
入门指南:
新用户可能想先浏览一下Hibernate入门指南,了解基本信息和教程。还有一系列专题指南,深入探讨各种主题。
快速入门地址:
https://docs.jboss.org/hibernate/orm/5.4/quickstart/html_single/
专题指南:
https://docs.jboss.org/hibernate/orm/5.4/topical/html_single/
原文:
Getting Started Guide
New users may want to first look through the Hibernate Getting Started Guide for basic information as well as tutorials.
There is also a series of topical guides providing deep dives into various topics.
提示信息:
虽然使用Hibernate并不需要有很强的SQL背景,但它确实有很大帮助,因为它都归结为SQL语句。可能更重要的是理解数据建模原则。您可能需要将这些资源视为一个良好的起点:
数据建模维基百科定义
数据建模101
理解事务和设计模式(如工作单元(PoEAA)或应用程序事务的基础知识也很重要。这些主题将在文档中讨论,但事先了解肯定会有所帮助。
While having a strong background in SQL is not required to use Hibernate, it certainly helps a lot because it all boils down to SQL statements.
Probably even more important is an understanding of data modeling principles.
You might want to consider these resources as a good starting point: Data modeling Wikipedia definition
Data Modeling 101
Understanding the basics of transactions and design patterns such as Unit of Work (PoEAA) or Application Transaction are important as well.
These topics will be discussed in the documentation, but a prior understanding will certainly help.
架构概况:
Hibernate作为一个ORM解决方案,有效地“位于”Java应用程序数据访问层和关系数据库之间,如上图所示。
Java应用程序使用hibernateapi来加载、存储、查询其域数据等。这里我们将介绍基本的hibernateapi。
这将是一个简短的介绍;我们稍后将详细讨论这些合同。
作为一个JPA提供者,Hibernate实现了Java持久性API规范,JPA接口和Hibernate特定实现之间的关联可以在下图中看到:
Hibernate, as an ORM solution, effectively "sits between" the Java application data access layer and the Relational Database, as can be seen in the diagram above.
The Java application makes use of the Hibernate APIs to load, store, query, etc. its domain data.
Here we will introduce the essential Hibernate APIs.
This will be a brief introduction; we will discuss these contracts in detail later. As a JPA provider, Hibernate implements the Java Persistence API specifications and the association between JPA interfaces and Hibernate specific implementations can be visualized in the following diagram:
会话工厂(org.hibernate.SessionFactory)
应用程序域模型到数据库的映射的线程安全(且不可变)表示。作为工厂org.hibernate.Session实例。EntityManagerFactory相当于一个SessionFactory,基本上,这两个集合在同一个SessionFactory实现中。
创建SessionFactory非常昂贵,因此,对于任何给定的数据库,应用程序应该只有一个关联的SessionFactory。
SessionFactory维护Hibernate跨所有会话使用的服务,例如二级缓存、连接池、事务系统集成等。
会话(org.hibernate.Session)
从概念上对“工作单元”(PoEAA)进行建模的单线程、短期对象。在JPA术语中,会话由EntityManager表示。
在幕后,Hibernate会话包装了一个JDBCjava.sql.Connection作为工厂org.hibernate.Transaction.事务实例。
它维护应用程序域模型的一般“可重复读取”持久性上下文(一级缓存)。
事务(org.hibernate.Transaction)
应用程序用来划分单个物理事务边界的单线程、短期对象。
EntityTransaction是JPA的等价物,两者都充当抽象API,将应用程序与正在使用的底层事务系统(JDBC或JTA)隔离开来。
1、SessionFactory (org.hibernate.SessionFactory)
A thread-safe (and immutable) representation of the mapping of the application domain model to a database.
Acts as a factory for org.hibernate.Session instances.
The EntityManagerFactory is the JPA equivalent of a SessionFactory and basically, those two converge into the same SessionFactory implementation. A SessionFactory is very expensive to create, so, for any given database, the application should have only one associated SessionFactory.
The SessionFactory maintains services that Hibernate uses across all Session(s) such as second level caches, connection pools, transaction system integrations, etc. 2、Session (org.hibernate.Session)
A single-threaded, short-lived object conceptually modeling a "Unit of Work" (PoEAA).
In JPA nomenclature, the Session is represented by an EntityManager. Behind the scenes, the Hibernate Session wraps a JDBC java.sql.Connection and acts as a factory for org.hibernate.Transaction instances.
It maintains a generally "repeatable read" persistence context (first level cache) of the application domain model. 3、Transaction (org.hibernate.Transaction)
A single-threaded, short-lived object used by the application to demarcate individual physical transaction boundaries.
EntityTransaction is the JPA equivalent and both act as an abstraction API to isolate the application from the underlying transaction system in use (JDBC or JTA).
在案例中多了一个注册对象的销毁处理:
package cn.zeal4j;
import cn.zeal4j.domain.News;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.MetadataSources;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.junit.Test;
import java.io.Serializable;
import java.util.Date; /**
* @author Administrator
* @file Hibernate
* @create 2020 09 23 16:16
*/
public class HibernateTest { @Test
public void quickStart() {
Transaction transaction = null;
Session session = null;
SessionFactory sessionFactory = null; // 但在5.1.0版本汇总,hibernate则采用如下新方式获取:
// 1. 配置类型安全的准服务注册类,这是当前应用的单例对象,不作修改,所以声明为final
// 在configure("cfg/hibernate.cfg.xml")方法中,如果不指定资源路径,默认在类路径下寻找名为hibernate.cfg.xml的文件
final StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure("hibernate/hibernate.cfg.xml").build(); try { // 2. 根据服务注册类创建一个元数据资源集,同时构建元数据并生成应用一般唯一的的session工厂
sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory(); /* - - - - - - - 上面是配置准备,下面开始我们的数据库操作 - - - - - - - */
session = sessionFactory.openSession(); //从会话工厂获取一个session transaction = session.beginTransaction(); News news = new News(null, "新闻标题:这是一段演示文本...", "作者:Zeal4J", new Date()); Serializable save = session.save(news); System.out.println("操作结果:" + save); transaction.commit(); } catch (Exception exception) {
exception.printStackTrace();
transaction.rollback();
// 通常情况会话工厂完成注册对象的销毁,但是会话工厂的建造出现异常,就需要手动销毁了
StandardServiceRegistryBuilder.destroy(registry);
} finally {
session.close();
sessionFactory.close();
}
}
}
【Hibernate】Re02 官网介绍的更多相关文章
- hadoop官网介绍及如何下载hadoop(2.4)各个版本与查看hadoop API介绍
1.如何访问hadoop官网?2.如何下载hadoop各个版本?3.如何查看hadoop API? 很多同学开发都没有二手资料,原因很简单觉得不会英语,但是其实作为软件行业,多多少少大家会英语的,但是 ...
- vue3官网介绍,安装,创建一个vue实例
前言:这一章主要是vue的介绍.安装.以及如何创建一个vue实例. 一.vue介绍 vue3中文官网:建议先自己看官网. https://v3.cn.vuejs.org/ vue是渐进式框架,渐进式指 ...
- Gearman研习笔记(1) ------ 官网介绍要点摘录
之前的项目里使用过消息中间件(公司提供的MQ服务)来做分发,因为MQ是基于消息的,并不是专业的任务分发器,在一些复杂场景上使用起来并不恰当. 后来听组长说了下Gearman(听名字还以为是Ironma ...
- Nordic老版官网介绍(2018-11-30停止更新)
1. Nordic官网及资料下载 Nordic官网主页:https://www.nordicsemi.com/,进入官网后,一般点击“Products”标签页,即进入Nordic产品下载首页,其独立链 ...
- 跟着minium官网介绍学习minium-----(一)
某天,再打开微信开发者工具的时候收到一条推送.说是微信小程序自动化框架 Python 版 -- Minium 公测. Url如下: https://developers.weixin.qq.com/c ...
- 跟着minium官网介绍学习minium-----(二)
一: 进入minium官方文档 1. 进入minium目录然后运行服务,出现以下提示说明打开成功, 2. 浏览器直接运行http://localhost:3000即可看到效果. 3. 下图为进入网页后 ...
- python爬虫 beutifulsoup4_1官网介绍
http://www.crummy.com/software/BeautifulSoup/bs4/doc/ Beautiful Soup Documentation Beautiful Soup is ...
- 微软自己的官网介绍 SSL 参数相关
https://docs.microsoft.com/en-us/dotnet/api/system.security.authentication.sslprotocols?redirectedfr ...
- AccessibilityService 官网介绍
AccessibilityService extends Service java.lang.Object ↳ android.content.Context ↳ android.co ...
- 跟着minium官网介绍学习minium-----(三)
注意:程序运行时在微信开发者工具当前页面为主,而不是每次运行都是从home页面开始 一 获取单个元素 get_element():在当前页面查询控件, 如果匹配到多个结果, 则返回第一个匹配到的结果 ...
随机推荐
- 跨域问题CORS笔记
CORS跨域问题 跨域问题简介 跨域资源共享(Cross-origin resource sharing, CORS)是用于让网站资源能被不同源网站访问的一种安全机制,这个机制由浏览器与服务器共同负责 ...
- JSR303数据校验使用方法记录
JSR303并不对应着指定的jar包,而是一种规范,目前hibernate-validator是使用最多的是基于JSR303规范的实现 本文不适合新人观看,要求至少要知道使用方法 Springboot ...
- Java面试知识点(四)重写和重载
重写override 在 java 中有很多的继承,继承下来的有变量.方法.在有一些子类要实现的方法中,方法名.传的参数.返回值跟父类中的方法一样,但具体实现又跟父类的不一样,这时候我们就需要重写父类 ...
- Java面试知识点(三)Java中的单继承和多继承
多继承的优缺点 优点:对象可以调用多个父类中的方法 缺点:如果派生类所继承的多个父类有相同的父类(也就是一个菱形继承结构),而派生类对象需要调用这个祖先类的方法,就会容易出现二义性. 1.java 与 ...
- 【路径规划】 The Dynamic Window Approach to Collision Avoidance (附python代码实例)
引用与前言 参考链接 引用参考如下: 博客园解释:https://www.cnblogs.com/dlutjwh/p/11158233.html 这篇博客园写的贼棒!我当时就是一边对着论文一边对着他这 ...
- 微信小程序day04基础加强
一.自定义组件 1.1 组件的创建与引用 首先创建组件 然后我们组件的引用分为局部和全局引用 局部引用就是在当前页面能使用,在当前页面的json文件里面配置 全局引用同样的道理,注意跟page等是同级 ...
- java开发webservice报Service(URL, QName, WebServiceFeature[]) is undefined错误的解决方法
Description Resource Path Location TypeThe constructor Service(URL, QName, WebServiceFeature[]) is u ...
- debian11 简单搭建go环境
简单环境,目前仅支持单版本go,后续可以考虑直接把go环境放到docker中或podman中,这样每个容器都是一套go版本. 新建文件夹目录 # 我直接用的root账户 cd /root mkdir ...
- Exception in thread "main" java.lang.NoClassDefFoundError: io/netty/channel/EventLoopGroup
最近在学习dubbo,跟着教程做,但是运行时报错,需要添加netty依赖 <dependency> <groupId>io.netty</groupId> < ...
- 教你基于MindSpore用DCGAN生成漫画头像
本文分享自华为云社区<[昇思25天学习打卡营打卡指南-第二十天]DCGAN生成漫画头像>,作者:JeffDing. DCGAN生成漫画头像 在下面的教程中,我们将通过示例代码说明DCGAN ...