马士兵hibernate源码
㈠ 怎么查看hibernate源码
方法/步骤
进入官网:hibernate.org
2、在跳转之后的页面中点击“Downloads”
3、至此jar包的下载就完成
4、下载源码包,点击“Source code”,之后会跳转到github网站上 :
5、点击右下角的“Download ZIP”至此,源码包也下载完毕
㈡ 如何学习hibernate源码
学习 Hibernate 源码一
1. 下载 hibernate 源码,并构建起 eclipse 项目
( 1 ) hibernate 的源代码采用 git 管理,安装 git 客户端以后,通过点击 github.com/hibernate/hibernate-orm 页面右侧的“ Clone in Desktop ”可启动客户端将代码 clone 到本地。当然也可以直接使用
git clone git://github.com/hibernate/hibernate-orm.git
命令下载代码。
下载完毕后,打开根目录下的 readme 文件,可以看到详细的把源代码构建为不同 IDE 项目的说明;以及其他的一些信息。
对应文档的网站链接:
community.jboss.org/wiki/BuildingHibernateORM4x5x
构建 IDE 部分:
Eclipse
To create the Eclipse project files you can run
After changes to the dependencies you need to clean the project files and recreate them:
./gradlew cleanEclipse eclipse
See also Contributing to Hibernate using Eclipse
Idea
To create the Idea project files you can run
./gradlew idea
After changes to the dependencies you need to clean the project files and recreate them:
./gradlew cleanIdea idea
此处的 gradle 是 hibernate 所使用的自动构建工具,官网地址为行基:
.gradle.org/
下载地址为: .gradle.org/downloads ,选择一个版本进行下载
此处是 Hibernate 团队阐述其从 Maven 迁移到 Gradle 的原因: community.jboss.org/wiki/GradleWhy
裤腔至于 grable ,有时间会深入学习一下,感兴趣的同学可以看看这位博主的博客:
.blogjava.net/wldandan/archive/2012/06/26/381532.html
言归正传,下载 grable 后,解压,我将胡带衫其解压到了 C 盘根目录,解压完毕的 grable 所在目录为: C:\gradle-1.7 ,将其子目录 C:\gradle-1.7\bin 目录加入系统的 Path 变量
编译代码为 eclipse 项目:
( 1 )进入 hibernate 源码的根目录:
cd C:\Users\Administrator\Documents\GitHub\hibernate-orm
( 2 )执行 gradlew.bat eclipse
接着会自动下载项目所需要的依赖项,具体输出如下:
C:\Users\Administrator\Documents\GitHub\hibernate-orm >gradlew.bat eclipse
Creating properties on demand (a.k.a. dynamic properties) has been deprecated and is scheled to be removed in Gradle 2.0. Please read gradle.org/docs/current/dsl/org.gradle.api.plugins.ExtraPropertiesExtension.html for information on
Deprecated dynamic property: "exportPackageVersion" on "project ':documentation'", value: "4.3.0".
Deprecated dynamic property "exportPackageVersion" created in multiple locations.
FAILURE: Build failed with an exception.
* Where:
Build file 'C:\Users\Administrator\Documents\GitHub\hibernate-orm\build.gradle' line: 85
* What went wrong:
A problem occurred evaluating root project 'hibernate-orm'.
> org/hibernate/build/gradle/publish/auth/maven/AuthenticationManager : Unsupported major.minor version 51.0
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
此时我去查看我的环境变量, JAVA_HOME 指向的是 64 位的 JDK6 ,将其指向 32 位 JDK7 后再次执行
gradlew.bat eclipse
发现此时 gradlew 又去下载了其他的依赖包,查看 hibernate-rom 目录下的 libraries.gradle 文件,可以看到 hibernate 的依赖包。
重新编译过程中还是报错了,具体错误:
:hibernate-core:compileJava
警告 : [options] 未与 -source 1.6 一起设置引导类路径
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\Loader.java:38: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\ResultCheckStyle.java:29: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLDelete.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLDeleteAll.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLInsert.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotations\SQLUpdate.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
注 : 某些输入文件使用或覆盖了已过时的 API 。
注 : 有关详细信息 , 请使用 -Xlint:deprecation 重新编译。
注 : 某些输入文件使用了未经检查或不安全的操作。
注 : 有关详细信息 , 请使用 -Xlint:unchecked 重新编译。
1 个警告
google 了一下,貌似是区域语言设置的问题,需要修改为“英语(英国)”,但编译并没用中断,接着编译。我将控制面板中的区域语言设置修改后,继续观察,看其是否还会报同样的错误。
最终还是失败了:
:hibernate-entitymanager:compileJava
警告 : [options] 未与 -source 1.6 一起设置引导类路径
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-entitymanager\src\main\java\org\hibernate\jpa\AvailableSettings.java:290: 错误 : 编码 GBK 的不可映射字符
* contains 钬淐 REATE SCHEMA 钬 ?commands. If this property is not supplied (or is explicitly {@code false}), the
^
注 : 某些输入文件使用或覆盖了已过时的 API 。
注 : 有关详细信息 , 请使用 -Xlint:deprecation 重新编译。
注 : 某些输入文件使用了未经检查或不安全的操作。
注 : 有关详细信息 , 请使用 -Xlint:unchecked 重新编译。
㈢ Hibernate框架ORM的实现原理
在目前来看,大多数语言都是基于面向对象的,在项目不同的层次,都是以对象来传递数据,而现在的数据库系统都是关系型数据库。所以我们在进行数据库操作时,比如向数据库表中添加一条记录,就会遍历对象的的每个属性进行添加操作。如果使用Hibernate,那么我们就可以直接传递一个对象给它,由Hibernate管理、解析,执行相应的SQL操作。
那么Hibernate是怎样实现呢?主要是依据反射机制。
现在以一次数据库查询操作分析Hibernate实现原理。
假设有一个用户表(tbl_user),表中字段有id,name,sex。同时有一个实体类(User)与其相对应,查询语句是: select * from User。
1.在项目启动时,Hibernate配置文件中的内容已经存储在容器中,存储着表与实体中的关系。
2.在执行select * from User 时,会根据反射机制先找到User的全路径名称,进而找到容器中User对应的配置。
3.由于配置文件中的实体属性与数据库中的字段是对应的,Hibernate会将select * from User 这个hql语句根据不同的数据库方言解析成不同的SQL语句(select * from tbl_user)。
大致过程就是这样,当然,器内部实现的具体过程是比较复杂的,在使用Hibernate进行数据库操作时,应注意级联、延迟加载、缓存的使用。