首先要感谢满江红社区:)给我们翻译了大量高质量的文档,我学习hibernate的文档资料主要来自他们的贡献。
对Hibernate概念模糊的一定要去看看 夏昕 写的《Hibernate 开发指南 》
一。Hibernate最小依赖包
手动添加每个项目的最小依赖包,并认识每个jar的大体作用是很有必要的:)
二、加载方式与核心类
1.加载Session
Session是持久层操作的基础,相当于JDBC中的 Connection。(注意Hibernate3中使用了与Hibernate2中不同的Session。老式的Session被迁移到org.hibernate.classic.Session)
Hibernate3以后应该使用如下的加载代码,可以获得绑定线程的Session变量。
- public void getSession() {
- try {
- Configuration cfg = new Configuration().configure();
- SessionFactory sf = cfg.buildSessionFactory();
- session = sf.getCurrentSession();
- } catch (HibernateException e) {
- e.printStackTrace();
- }
- }
三。查询结构:
用一个RBAC的权限模型的一部分(USER,ROLE,USER_TO_ROLE )做例子
POJO如下:
- package model;
- import java.util.HashSet;
- import java.util.Set;
- public class User implements java.io.Serializable {
-
- private Integer userId;
- private Department department;
- private String loginId;
- private String password;
- private String name;
- private Set userToRoles = new HashSet(0);
-
-
- public User() {
- }
-
-
- public User(Department department, String loginId, String password, String name, Set userToRoles) {
- this.department = department;
- this.loginId = loginId;
- this.password = password;
- this.name = name;
- this.userToRoles = userToRoles;
- }
-
-
- public Integer getUserId() {
- return this.userId;
- }
-
- public void setUserId(Integer userId) {
- this.userId = userId;
- }
- public Department getDepartment() {
- return this.department;
- }
-
- public void setDepartment(Department department) {
- this.department = department;
- }
- public String getLoginId() {
- return this.loginId;
- }
-
- public void setLoginId(String loginId) {
- this.loginId = loginId;
- }
- public String getPassword() {
- return this.password;
- }
-
- public void setPassword(String password) {
- this.password = password;
- }
- public String getName() {
- return this.name;
- }
-
- public void setName(String name) {
- this.name = name;
- }
- public Set getUserToRoles() {
- return this.userToRoles;
- }
-
- public void setUserToRoles(Set userToRoles) {
- this.userToRoles = userToRoles;
- }
-
- }
- package model;
- import java.util.HashSet;
- import java.util.Set;
- public class Role implements java.io.Serializable {
-
- private Integer roleId;
- private String roleName;
- private String comment;
- private Set userToRoles = new HashSet(0);
- private Set roleToResources = new HashSet(0);
-
-
- public Role() {
- }
-
- public Role(String roleName, String comment, Set userToRoles,
- Set roleToResources) {
- this.roleName = roleName;
- this.comment = comment;
- this.userToRoles = userToRoles;
- this.roleToResources = roleToResources;
- }
-
- public Integer getRoleId() {
- return this.roleId;
- }
- public void setRoleId(Integer roleId) {
- this.roleId = roleId;
- }
- public String getRoleName() {
- return this.roleName;
- }
- public void setRoleName(String roleName) {
- this.roleName = roleName;
- }
- public String getComment() {
- return this.comment;
- }
- public void setComment(String comment) {
- this.comment = comment;
- }
- public Set getUserToRoles() {
- return this.userToRoles;
- }
- public void setUserToRoles(Set userToRoles) {
- this.userToRoles = userToRoles;
- }
- public Set getRoleToResources() {
- return this.roleToResources;
- }
- public void setRoleToResources(Set roleToResources) {
- this.roleToResources = roleToResources;
- }
- }
- package model;
- public class UserToRole implements java.io.Serializable {
-
- private Integer urId;
- private User user;
- private Role role;
- private Integer projectId;
-
-
- public UserToRole() {
- }
-
- public UserToRole(User user, Role role, Integer projectId) {
- this.user = user;
- this.role = role;
- this.projectId = projectId;
- }
-
- public Integer getUrId() {
- return this.urId;
- }
- public void setUrId(Integer urId) {
- this.urId = urId;
- }
- public User getUser() {
- return this.user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- public Role getRole() {
- return this.role;
- }
- public void setRole(Role role) {
- this.role = role;
- }
- public Integer getProjectId() {
- return this.projectId;
- }
- public void setProjectId(Integer projectId) {
- this.projectId = projectId;
- }
- }
生成的映射为:
User.hbm.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="model.User" table="user" catalog="gov">
- <id name="userId" type="java.lang.Integer">
- <column name="USER_ID" />
- <generator class="identity" />
- </id>
- <many-to-one name="department" class="model.Department" fetch="select">
- <column name="DEPARTMENT_ID">
- <comment>ËùÔÚ²¿ÃÅ</comment>
- </column>
- </many-to-one>
- <property name="loginId" type="java.lang.String">
- <column name="LOGIN_ID" length="20" />
- </property>
- <property name="password" type="java.lang.String">
- <column name="PASSWORD" length="20" />
- </property>
- <property name="name" type="java.lang.String">
- <column name="NAME" length="20" />
- </property>
- <set name="userToRoles" inverse="true">
- <key>
- <column name="USER_ID" />
- </key>
- <one-to-many class="model.UserToRole" />
- </set>
- </class>
- </hibernate-mapping>
Role.hbm.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="model.Role" table="role" catalog="gov">
- <id name="roleId" type="java.lang.Integer">
- <column name="ROLE_ID" />
- <generator class="identity" />
- </id>
- <property name="roleName" type="java.lang.String">
- <column name="ROLE_NAME" length="40">
- <comment>ְλÃû</comment>
- </column>
- </property>
- <property name="comment" type="java.lang.String">
- <column name="COMMENT" length="200">
- <comment>ְλÃèÊö</comment>
- </column>
- </property>
- <set name="userToRoles" inverse="true">
- <key>
- <column name="ROLE_ID" />
- </key>
- <one-to-many class="model.UserToRole" />
- </set>
- <set name="roleToResources" inverse="true">
- <key>
- <column name="ROLE_ID" />
- </key>
- <one-to-many class="model.RoleToResource" />
- </set>
- </class>
- </hibernate-mapping>
UserToRole.hbm.xml
- <?xml version="1.0" encoding="utf-8"?>
- <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping>
- <class name="model.UserToRole" table="user_to_role" catalog="gov">
- <id name="urId" type="java.lang.Integer">
- <column name="UR_ID" />
- <generator class="identity" />
- </id>
- <many-to-one name="user" class="model.User" fetch="select">
- <column name="USER_ID" />
- </many-to-one>
- <many-to-one name="role" class="model.Role" fetch="select">
- <column name="ROLE_ID" />
- </many-to-one>
- <property name="projectId" type="java.lang.Integer">
- <column name="PROJECT_ID" />
- </property>
- </class>
- </hibernate-mapping>
下面做了个TestCase。分别描述了HQL和SQL方式的查询,至于Criteria方式虽然比较面向对象,但是我不是很习惯:)
- package test;
- import java.util.List;
- import model.Role;
- import org.hibernate.HibernateException;
- import org.hibernate.SQLQuery;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.type.IntegerType;
- import org.junit.After;
- import org.junit.Before;
- import org.junit.Test;
- public class TestCase {
- Session session = null;
- @Before
-
-
- public void getSession() {
- try {
- Configuration cfg = new Configuration().configure();
- SessionFactory sf = cfg.buildSessionFactory();
- session = sf.getCurrentSession();
- } catch (HibernateException e) {
- e.printStackTrace();
- }
- }
-
- @Test
- public void test() {
- session.beginTransaction();
-
- String hql_findRole = "select role.roleId from UserToRole";
- List list1 = session.createQuery(hql_findRole).list();
-
- session.getTransaction().commit();
- }
-
-
- @Test
- public void SqlTest1() {
- session.beginTransaction();
- String sql_findRoleId = "select role_id from gov.user_to_role";
- SQLQuery sq2 = session.createSQLQuery(sql_findRoleId).addScalar(
- "role_id", new IntegerType());
- List list2 = sq2.list();
- session.getTransaction().commit();
- }
-
-
- @Test
- public void sqlTest2() {
- session.beginTransaction();
- String sql_findRole = "select * from gov.role";
- SQLQuery sq3 = session.createSQLQuery(sql_findRole).addEntity(
- Role.class);
- List list3 = sq3.list();
-
- session.getTransaction().commit();
- }
- @After
- public void clearDown() {
- try {
-
-
-
-
-
-
- } catch (HibernateException e) {
- e.printStackTrace();
- }
- }
- }
分享到:
相关推荐
1. **异常处理**: 在描述中提到的`org.hibernate.PropertyValueException: not-null property references a null or transient value`是Hibernate抛出的一个常见异常,表示尝试保存一个实体时,其某个非空属性为...
### Hibernate精华笔记全纪录 #### 一、知识准备与背景介绍 **1. JDBC基础** 在深入了解Hibernate之前,首先需要掌握Java Database Connectivity (JDBC)的基础知识。JDBC是Java中用于连接各种类型数据库的标准API...
在深入探讨《hibernate笔记.txt》所提及的关键知识点前,我们先来解析一下标题和描述中的核心概念。“自上而下的依赖,单向依赖,层与层之间最好依赖抽象”,这一描述实际上触及了软件架构设计中的关键原则,特别是...
《韩顺平.2011版.hibernate笔记》是一份针对Hibernate框架的详细学习资料,由知名IT讲师韩顺平在2011年编撰而成。Hibernate是Java开发领域中广泛使用的对象关系映射(ORM)框架,它极大地简化了数据库操作,使开发者...
【传智播客 Hibernate 笔记】是一份深入学习 Hibernate ORM 框架的资源集合,包括了 word 笔记、pdf 讲义、源代码以及相关分析图表和 jar 包,旨在帮助开发者全面理解并掌握 Hibernate 的核心概念与实际应用。...
《Hibernate笔记完整版》 在Web开发领域,Hibernate作为三大框架之一,扮演着至关重要的角色。本文将全面介绍Hibernate,包括其数据持久化的概念、优缺点对比,以及Hibernate的核心概念和编程步骤。 对象持久化是...
《韩顺平Hibernate笔记》是一份详尽记录了著名IT讲师韩顺平关于Hibernate框架讲解内容的资料集合。Hibernate作为Java领域中广泛使用的对象关系映射(ORM)框架,极大地简化了数据库操作,使得开发者可以更加专注于...
1. Configuration类:这是启动Hibernate的第一步,它负责配置并创建SessionFactory实例。配置包括数据库连接信息、ORM映射文件路径等。 2. SessionFactory接口:这是Hibernate的核心组件,它是线程安全的,用于初始...
Hibernate学习笔记整理 以下是 Hibernate 框架的详细知识点: Hibernate 介绍 Hibernate 是一个 ORM(Object-Relational Mapping)框架,用于将 Java 对象映射到数据库表中。它提供了一个简洁的方式来访问和操作...
《韩顺平Hibernate笔记》是一份详尽的关于Hibernate框架的学习资料,由知名IT教育专家韩顺平编撰。Hibernate是Java开发中的一个强大的对象关系映射(ORM)框架,它简化了数据库与Java对象之间的交互,使得开发者可以...
Hibernate学习笔记 Hibernate学习笔记 Hibernate学习笔记 Hibernate学习笔记
培训期间的hibernate笔记 hibernate笔记 达内培训
### 马士兵Hibernate学习笔记知识点总结 #### 一、课程内容概览 - **HelloWorld**:通过简单的示例程序介绍如何启动一个基于Hibernate的Java项目。 - **Hibernate原理模拟**:解释O/R Mapping的概念及其重要性。 -...
### 尚学堂Hibernate笔记知识点详解 #### 一、项目初始化与环境搭建 1. **新建项目**:在IDE中创建一个新的Java项目。 2. **构建用户库并添加所需的JAR包**: - 右键点击项目 -> `Build Path` -> `Configure ...
hibernate的详细笔记 基础配置
《Hibernate学习笔记特别详细》 Hibernate,作为一款开源的Object-Relational Mapping(ORM)框架,为Java开发者提供了强大的数据库操作支持。它简化了数据库访问的复杂性,使得开发人员可以像操作对象一样操作...