hibernate框架源码分析(hibernate 查询操作 帮忙完善下 或提供下源码感激不尽)
本文目录
- hibernate 查询操作 帮忙完善下 或提供下源码感激不尽
- hibernate 的源代码的原理是什么
- 如何学习hibernate源码
- Hibernate框架ORM的实现原理
- NHibernate源码的异常处理方法
- 求对hibernate源码了解的大神!关于hibernate4及以上版本---不配置事务,dao类继承自HibernateDaoSupport
hibernate 查询操作 帮忙完善下 或提供下源码感激不尽
public String selectAall() {
List《Blog》 list=null;
try {
session = sf.openSession();
Query query=session.createQuery("from Blog");
list=query.list();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.close();
sf.close();
}
request.setAttribute("list",list);//将list保存至request中,就可以在转入的jsp页面取到
return "要跳转的页面";//或者使用requestDispatcher.forward("");
}
hibernate 的源代码的原理是什么
下面这个里面有,不过告诉你的是
struts hibernate 不是Structs hibernate
如何学习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 scheduled 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 locati***.
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.
此时我去查看我的环境变量, J**A_HOME 指向的是 64 位的 JDK6 ,将其指向 32 位 JDK7 后再次执行
gradlew.bat eclipse
发现此时 gradlew 又去下载了其他的依赖包,查看 hibernate-rom 目录下的 libraries.gradle 文件,可以看到 hibernate 的依赖包。
重新编译过程中还是报错了,具体错误:
:hibernate-core:compileJava
警告 : 未与 -source 1.6 一起设置引导类路径
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotati***\Loader.java:38: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotati***\ResultCheckStyle.java:29: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotati***\**LDelete.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotati***\**LDeleteAll.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotati***\**LInsert.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
C:\Users\Administrator\Documents\GitHub\hibernate-orm\hibernate-core\src\main\java\org\hibernate\annotati***\**LUpdate.java:37: 错误 : 编码 GBK 的不可映射字符
* @author L 锟絪 zl 锟 ?Benke
^
注 : 某些输入文件使用或覆盖了已过时的 API 。
注 : 有关详细信息 , 请使用 -Xlint:deprecation 重新编译。
注 : 某些输入文件使用了未经检查或不安全的操作。
注 : 有关详细信息 , 请使用 -Xlint:unchecked 重新编译。
1 个警告
google 了一下,貌似是区域语言设置的问题,需要修改为“英语(英国)”,但编译并没用中断,接着编译。我将控制面板中的区域语言设置修改后,继续观察,看其是否还会报同样的错误。
最终还是失败了:
:hibernate-entitymanager:compileJava
警告 : 未与 -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管理、解析,执行相应的**L操作。
那么Hibernate是怎样实现呢?主要是依据反射机制。
现在以一次数据库查询操作分析Hibernate实现原理。
假设有一个用户表(tbl_user),表中字段有id,name,***。同时有一个实体类(User)与其相对应,查询语句是: select * from User。
1.在项目启动时,Hibernate配置文件中的内容已经存储在容器中,存储着表与实体中的关系。
2.在执行select * from User 时,会根据反射机制先找到User的全路径名称,进而找到容器中User对应的配置。
3.由于配置文件中的实体属性与数据库中的字段是对应的,Hibernate会将select * from User 这个hql语句根据不同的数据库方言解析成不同的**L语句(select * from tbl_user)。
大致过程就是这样,当然,器内部实现的具体过程是比较复杂的,在使用Hibernate进行数据库操作时,应注意级联、延迟加载、缓存的使用。
NHibernate源码的异常处理方法
NHibernate所有的异常处理都派生自HibernateException类,发生在数据库层的原生异常被保留,没做处理。
HibernateException类派生自系统的ApplicationException类,ApplicationException解释如下
The exception that is thrown when a non-fatal application error occurs.
抛出一个非致命的程序异常。HibernateException类的Serializable关键字,表明可以被序列化,可以保存成文件下来以做分析。
系统中很多异常处理类,发生重要错误或异常时用log4net记录下信息。
看看NHibernate在ado.net事务提交时的异常处理方法:
public void Commit()
{
using (new SessionIdLoggingContext(sessionId))
{
CheckNotDisposed();
CheckBegun();
CheckNotZombied();
log.Debug("Start Commit");
if (session.FlushMode != FlushMode.Never)
{
session.Flush();
}
NotifyLocalSynchsBeforeTransactionCompletion();
session.BeforeTransactionCompletion(this);
try
{
trans.Commit();
log.Debug("IDbTransaction Committed");
committed = true;
AfterTransactionCompletion(true);
Dispose();
}
catch (HibernateException e)
{
log.Error("Commit failed", e);
AfterTransactionCompletion(false);
commitFailed = true;
// Don’t wrap HibernateExcepti***
throw;
}
catch (Exception e)
{
log.Error("Commit failed", e);
AfterTransactionCompletion(false);
commitFailed = true;
throw new TransactionException("Commit failed with **L exception", e);
}
finally
{
CloseIfRequired();
}
}
}
其中 Don’t wrap HibernateExcepti***表明这样处理不会清掉已经发生的HibernateExcepti***信息。就是在执行过程中抓到了其他的HibernateExcepti***类型的异常就向上抛。这就是自定义异常处理的好处。Commit failed with **L exception表明数据库sql事务提交执行出错,向上抛出HibernateExcepti***类型的错误信息 ,因为都派生自HibernateException类。
同类就throw,不是同类就throw个message。
另外不好的地方就是,异常处理中没包括标识信息,使上层调用无法通过标识信息确定到底发什么了什么异常,只能看到一个message。 这个只有通过修改原码来实现。就是把一个个的标识加到一个异常message中,以便上层抓到。
求对hibernate源码了解的大神!关于hibernate4及以上版本---不配置事务,dao类继承自HibernateDaoSupport
你需要将hibernate的原码给关联起来,按照他的提示即AttatchSource,一般下载的jar包里面貌似三有src.zip这个文件,或者你在项目lib里面找到这个类,当然是按照路径来咯,这个比较麻烦,不知道你设置过jdk的源码没有,过程跟那个差不多
更多文章:
查看进程打开的文件句柄(linux如何获取某个进程打开的句柄数,c代码实现)
2026年4月21日 01:40
tablet pc驱动程序下载(谁有windows xp TABLET pc edition啊!跪求下载地址!)
2026年4月21日 01:00
route add net(Linux-linux 下用route命令怎么设置网关)
2026年4月21日 00:40
hibernate框架源码分析(hibernate 查询操作 帮忙完善下 或提供下源码感激不尽)
2026年4月21日 00:20
menuitem控件(asp.net menu控件菜单项的间距如何调)
2026年4月21日 00:00
java正则表达式大括号(正则表达式中的()[]{}这三种括号一般有什么区别)
2026年4月20日 23:40
vlookup函数不能自动填充(EXCEL函数vlookup 不能自动填充)
2026年4月20日 23:00
log4j appender(J**A的log4j如何实现程序重新启动会自动生成新的日志文件,文件名可以是当前日期)
2026年4月20日 22:40



![java正则表达式大括号(正则表达式中的()[]{}这三种括号一般有什么区别)](/static/images/nopic/4.jpg)
