查看“Hibernate源码分析:入门代码流程”的源代码
←
Hibernate源码分析:入门代码流程
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
[[category:Hibernate]] == 关于 == 对于入门的代码案例,分析其执行流程,有助于理解其设计并熟悉 API 的使用。 入门代码: : <syntaxhighlight lang="Java" highlight=""> class HibernateTest{ @Test public void testDemo() { // 加载 hibernate 核心配置文件 Configuration cfg = new Configuration(); cfg.configure(); // 根据 hibernate 核心配置文件内容,创建 sessionFactory SessionFactory sessionFactory = cfg.buildSessionFactory(); // 使用 SessionFactory 创建 session 对象 Session session = sessionFactory.openSession(); // Session session = sessionFactory.grtCurrentSession(); // 使用事务 Transaction tx = null; try{ // 开启事务 tx = session.beginTransaction(); // 逻辑代码 User user = new User(); user.setUsername("Eijux"); user.setPassword("123456"); user.setAddress("成都"); session.save(user); // 提交事务 tx.commit(); }catch(HibernateException e){ if(tx != null){ // 回滚事务 tx.rollback(); } throw e; }finally{ if(session != null){ // 关闭 session session.close(); } } // 关闭 sessionFactory sessionFactory.close(); } } </syntaxhighlight> == 加载核心配置文件 == 对于: : <syntaxhighlight lang="Java" highlight=""> Configuration cfg = new Configuration(); cfg.configure(); </syntaxhighlight> 其过程如下: # 创建一个 Configuration 对象; # 调用其 configure() 方法: #: <syntaxhighlight lang="Java" highlight=""> public class Configuration { ... public Configuration() { this( new BootstrapServiceRegistryBuilder().build() ); } public Configuration(BootstrapServiceRegistry serviceRegistry) { this.bootstrapServiceRegistry = serviceRegistry; this.metadataSources = new MetadataSources( serviceRegistry ); reset(); } public Configuration(MetadataSources metadataSources) { this.bootstrapServiceRegistry = getBootstrapRegistry( metadataSources.getServiceRegistry() ); this.metadataSources = metadataSources; reset(); } ... } </syntaxhighlight> #* 有多个重载的构造方法,用于不同加载方式。 #* 其实默认的无参构造函数,将创建一个 BootstrapServiceRegistryImpl 类(继承了 BootstrapServiceRegistryBuilder 类)的对象。 【待续……】 == 创建 sessionFactory == == 创建 session == == 事务 ==
返回至“
Hibernate源码分析:入门代码流程
”。
导航菜单
个人工具
登录
命名空间
页面
讨论
大陆简体
已展开
已折叠
查看
阅读
查看源代码
查看历史
更多
已展开
已折叠
搜索
导航
首页
最近更改
随机页面
MediaWiki帮助
笔记
服务器
数据库
后端
前端
工具
《To do list》
日常
阅读
电影
摄影
其他
Software
Windows
WIKIOE
所有分类
所有页面
侧边栏
站点日志
工具
链入页面
相关更改
特殊页面
页面信息