首先要感谢满江红社区:)给我们翻译了大量高质量的文档,我学习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();
- }
- }
- }
分享到:
相关推荐
虚拟机vmware workstatiions pro 17.6.1个人使用免费,不用证书直接安装使用
scipy-1.2.0-cp35-cp35m-linux_armv7l.whl
基于springboot爬虫高考志愿智能推荐系统源码数据库文档.zip
bimdata_api_client-4.3.0-py3-none-any.whl
Pillow-7.0.0-cp37-cp37m-linux_armv7l.whl
onnxruntime-1.16.2-cp38-cp38-win_amd64.whl
opencv_python-4.4.0.46-cp39-cp39-linux_armv7l.whl
论文描述:该论文研究了某一特定领域的问题,并提出了新的解决方案。论文首先对问题进行了详细的分析和理解,并对已有的研究成果进行了综述。然后,论文提出了一种全新的解决方案,包括算法、模型或方法。在整个研究过程中,论文使用了合适的实验设计和数据集,并进行了充分的实验验证。最后,论文对解决方案的性能进行了全面的评估和分析,并提出了进一步的研究方向。 源码内容描述:该源码实现了论文中提出的新的解决方案。源码中包含了算法、模型或方法的具体实现代码,以及相关的数据预处理、实验设计和性能评估代码。源码中还包括了合适的注释和文档,以方便其他研究者理解和使用。源码的实现应该具有可读性、可维护性和高效性,并能够复现论文中的实验结果。此外,源码还应该尽可能具有通用性,以便在其他类似问题上进行进一步的应用和扩展。
ta_lib-0.5.1-cp313-cp313-win32.whl
STM32开发相关软件ISP 程序下载STM32开发相关软件ISP 程序下载提取方式是百度网盘分享地址
java源码资源基于JAVA的UDP服务器模型源代码提取方式是百度网盘分享地址
基于Python+Django的电影票房数据分析系统源码数据库文档.zip
onnxruntime-1.19.0-cp311-cp311-win_amd64.whl
Java固定资产管理系统源码 固定资产管理系统是对高校固定资产的一个信息化管理系统,基本功能包括:对固定资产的购进、接触、销毁,对物品的使用状态、借出状态、库存状态等进行标识,对各类物品进行编号,根据编号进行查询,根据名称进行查询等。本系统结构如下: (1)系统登录:用户登录模块:登录功能 重置 (2)系统用户管理:对系统用户的增加 系统用户的权限修改 系统用户的删除 分配系统用户的权限 修改本身登录密码 资产的相关维护 (3)员工信息管理:教工的增加、修改、删除、查询 (4)资产入库管理:资产的属性修改 资产的报废删除 资产的属性查询 (5)资产维护管理:物资的维修、维护物资的信息修改 (6)资产借还管理:增加借出资产 查询借出资产 归还已借出资产(7)打印报表
matplotlib-3.7.0-cp311-cp311-linux_armv7l.whl
基于springboot的房屋租赁管理系统源码数据库文档.zip
opencv_contrib_python-4.1.0.25-cp35-cp35m-linux_armv7l.whl
bimdata_api_client-3.1.0-py3-none-any.whl
本23级物流01班谭敏20234970114.docx
opencv_contrib_python-4.5.2.52-cp39-cp39-linux_armv7l.whl