hibernate3源码
① 如何学习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源码
方法/步骤
进入官网:hibernate.org
2、在跳转之后的页面中点击“Downloads”
3、至此jar包的下载就完成
4、下载源码包,点击“Source code”,之后会跳转到github网站上 :
5、点击右下角的“Download ZIP”至此,源码包也下载完毕
③ 如何学习hibernate源码
我来分享一下查看源码的方法:
查看源码的首要任务是要有一款上手的工具,这里用的是 IDEA。IDEA 的功能比较强大,包括 查看类结构图,debug。这两个是查看源码的关键功能。
查看源码可以静态查看和动态查看,静态查看的方法是查看类图,还有 ALT + f7 查看方法在哪里被调用或者类在哪里被调用。在看 spring 源码的时候就是用这种方法,不过这种方法对阅读者的要求比较高,包括要了解这个方法的执行,设计模式的理解,以及框架是如何配置这个类的。第二种方法是 debug。debug 方法是后来才发现的一个重要的 查看源码的方法,要点是掌握执行栈,就能掌握整个执行流程。比如这个是在debug hibernate 源码的时候的截图,可以看到这个执行栈非常深,从 spring-data-jpa 到 hibernate 中间经过好几层的代理,主要完成一些适配,事务,拦截器等等操作,然后再到 hibernate 核心代码,最后就是 jdbc 的 statement。方法栈中的每一个方法都是可以查看的,里面的变量有时候是代理了好几层,所以要 F7 进去才能看到真正的执行类。
上面是简单的简述 mybatis 的 cache 机制的源码,真正想让读者明白的是,debug 如何查看源码,查看源码需要抓住一个主题,不然在阅读庞大的框架的时候会找不着北。
所以,阅读源码需要掌握工具使用,debug, 查看类图,查看方法在哪里调用,软知识是要掌握设计模式,对框架的概念有了解。