您好,欢迎访问一九零五行业门户网

Hibernate框架基础

orm概念 o, object 对象r, realtion 关系 (关系型数据库: mysql, oracle…)m,mapping 映射orm, 对象关系映射!orm, 解决什么问题? 存储: 能否把对象的数据直接保存到数据库? 获取: 能否直接从数据库拿到一个对象?想做到上面2点,必须要有映射!总结:
orm概念o, object 对象r, realtion 关系 (关系型数据库: mysql, oracle…)m,mapping 映射orm, 对象关系映射!orm, 解决什么问题? 存储: 能否把对象的数据直接保存到数据库? 获取: 能否直接从数据库拿到一个对象?想做到上面2点,必须要有映射!总结: hibernate与orm的关系? hibernate是orm的实现!
hibernate 案例搭建一个hibernate环境,开发步骤:1. 下载源码 版本:hibernate-distribution-3.6.0.final2. 引入jar文件 hibernate3.jar核心 + required 必须引入的(6个) + jpa 目录 + 数据库驱动包3. 写对象以及对象的映射 employee.java 对象 employee.hbm.xml 对象的映射 (映射文件)4. src/hibernate.cfg.xml 主配置文件 数据库连接配置 加载所用的映射(*.hbm.xml)5. app.java 测试
对象      employee.java
//一、 对象public class employee { private int empid; private string empname; private date workdate;}
对象的映射 employee.hbm.xml

主配置文件 hibernate.cfg.xml
com.mysql.jdbc.driver jdbc:mysql:///hib_demo root root org.hibernate.dialect.mysql5dialect true
测试类 app.java
public class app { @test public void testhello() throws exception { // 对象 employee emp = new employee(); emp.setempname(班长); emp.setworkdate(new date()); // 获取加载配置文件的管理类对象 configuration config = new configuration(); config.configure(); // 默认加载src/hibenrate.cfg.xml文件 // 创建session的工厂对象 sessionfactory sf = config.buildsessionfactory(); // 创建session (代表一个会话,与数据库连接的会话) session session = sf.opensession(); // 开启事务 transaction tx = session.begintransaction(); //保存-数据库 session.save(emp); // 提交事务 tx.commit(); // 关闭 session.close(); sf.close(); }}
hibernate api|-- configuration 配置管理类对象 config.configure(); 加载主配置文件的方法(hibernate.cfg.xml) 默认加载src/hibernate.cfg.xml config.configure(“cn/config/hibernate.cfg.xml”); 加载指定路径下指定名称的主配置文件 config.buildsessionfactory(); 创建session的工厂对象|-- sessionfactory session的工厂(或者说代表了这个hibernate.cfg.xml配置文件) sf.opensession(); 创建一个sesison对象 sf.getcurrentsession(); 创建session或取出session对象|--session session对象维护了一个连接(connection), 代表了与数据库连接的会话。 hibernate最重要的对象: 只用使用hibernate与数据库操作,都用到这个对象 session.begintransaction(); 开启一个事务; hibernate要求所有的与数据库的操作必须有事务的环境,否则报错! 更新: session.save(obj); 保存一个对象 session.update(emp); 更新一个对象 session.saveorupdate(emp); 保存或者更新的方法: 没有设置主键,执行保存; 有设置主键,执行更新操作; 如果设置主键不存在报错! 主键查询: session.get(employee.class, 1); 主键查询 session.load(employee.class, 1); 主键查询 (支持懒加载) hql查询: hql查询与sql查询区别: sql: (结构化查询语句)查询的是表以及字段; 不区分大小写。 hql: hibernate query language 即hibernate提供的面向对象的查询语言 查询的是对象以及对象的属性。 区分大小写。 criteria查询: 完全面向对象的查询。 本地sql查询: 复杂的查询,就要使用原生态的sql查询,也可以,就是本地sql查询的支持! (缺点: 不能跨数据库平台!)|-- transaction hibernate事务对象共性问题1:classnotfoundexception…., 缺少jar文件!共性问题2: 如果程序执行程序,hibernate也有生成sql语句,但数据没有结果影响。 问题一般是事务忘记提交…….遇到问题,一定看错误提示!
hibernate crudpublic class employeedaoimpl implements iemployeedao{ @override public employee findbyid(serializable id) { session session = null; transaction tx = null; try { // 获取session session = hibernateutils.getsession(); // 开启事务 tx = session.begintransaction(); // 主键查询 return (employee) session.get(employee.class, id); } catch (exception e) { throw new runtimeexception(e); } finally { tx.commit(); session.close(); } } @override public list getall() { session session = null; transaction tx = null; try { session = hibernateutils.getsession(); tx = session.begintransaction(); // hql查询 query q = session.createquery(from employee); return q.list(); } catch (exception e) { throw new runtimeexception(e); } finally { tx.commit(); session.close(); } } @override public list getall(string employeename) { session session = null; transaction tx = null; try { session = hibernateutils.getsession(); tx = session.begintransaction(); query q =session.createquery(from employee where empname=?); // 注意:参数索引从0开始 q.setparameter(0, employeename); // 执行查询 return q.list(); } catch (exception e) { throw new runtimeexception(e); } finally { tx.commit(); session.close(); } } @override public list getall(int index, int count) { session session = null; transaction tx = null; try { session = hibernateutils.getsession(); tx = session.begintransaction(); query q = session.createquery(from employee); // 设置分页参数 q.setfirstresult(index); // 查询的其实行 q.setmaxresults(count); // 查询返回的行数 list list = q.list(); return list; } catch (exception e) { throw new runtimeexception(e); } finally { tx.commit(); session.close(); } } @override public void save(employee emp) { session session = null; transaction tx = null; try { session = hibernateutils.getsession(); tx = session.begintransaction(); // 执行保存操作 session.save(emp); } catch (exception e) { throw new runtimeexception(e); } finally { tx.commit(); session.close(); } } @override public void update(employee emp) { session session = null; transaction tx = null; try { session = hibernateutils.getsession(); tx = session.begintransaction(); session.update(emp); } catch (exception e) { throw new runtimeexception(e); } finally { tx.commit(); session.close(); } } @override public void delete(serializable id) { session session = null; transaction tx = null; try { session = hibernateutils.getsession(); tx = session.begintransaction(); // 先根据id查询对象,再判断删除 object obj = session.get(employee.class, id); if (obj != null) { session.delete(obj); } } catch (exception e) { throw new runtimeexception(e); } finally { tx.commit(); session.close(); } }}
hibernate.cfg.xml 主配置hibernate.cfg.xml 主配置文件中主要配置:数据库连接信息、其他参数、映射信息!常用配置查看源码: hibernate-distribution-3.6.0.final\project\etc\hibernate.properties
数据库连接参数配置例如:## mysql#hibernate.dialect org.hibernate.dialect.mysqldialect#hibernate.dialect org.hibernate.dialect.mysqlinnodbdialect#hibernate.dialect org.hibernate.dialect.mysqlmyisamdialect#hibernate.connection.driver_class com.mysql.jdbc.driver#hibernate.connection.url jdbc:mysql:///test#hibernate.connection.username gavin#hibernate.connection.password
自动建表hibernate.properties
#hibernate.hbm2ddl.auto create-drop 每次在创建sessionfactory时候执行创建表; 当调用sesisonfactory的close方法的时候,删除表!#hibernate.hbm2ddl.auto create 每次都重新建表; 如果表已经存在就先删除再创建#hibernate.hbm2ddl.auto update 如果表不存在就创建; 表存在就不创建;#hibernate.hbm2ddl.auto validate (生成环境时候) 执行验证: 当映射文件的内容与数据库表结构不一样的时候就报错!
代码自动建表
public class app_ddl { // 自动建表 @test public void testcreate() throws exception { // 创建配置管理类对象 configuration config = new configuration(); // 加载主配置文件 config.configure(); // 创建工具类对象 schemaexport export = new schemaexport(config); // 建表 // 第一个参数: 是否在控制台打印建表语句 // 第二个参数: 是否执行脚本 export.create(true, true); }}
映射配置1. 普通字段类型2. 主键映射 单列主键映射 多列作为主键映射主键生成策略,查看api: 5.1.2.2.1. various additional generators数据库: 一个表能否有多个主键? 不能。 为什么要设置主键? 数据库存储的数据都是有效的,必须保持唯一。 (为什么把id作为主键?) 因为表中通常找不到合适的列作为唯一列即主键,所以为了方法用id列,因为id是数据库系统维护可以保证唯一,所以就把这列作为主键! 联合/复合主键 如果找不到合适的列作为主键,出来用id列以外,我们一般用联合主键,即多列的值作为一个主键,从而确保记录的唯一性。
映射配置
复合主键映射对象与主键
// 复合主键类public class compositekeys implements serializable{ private string username; private string address; // .. get/set}
public class user { // 名字跟地址,不会重复 private compositekeys keys; private int age;}
user.hbm.xml

app.java
public class app2 { private static sessionfactory sf; static { // 创建sf对象 sf = new configuration() .configure() .addclass(user.class) //(测试) 会自动加载映射文件:employee.hbm.xml .buildsessionfactory(); } //1. 保存对象 @test public void testsave() throws exception { session session = sf.opensession(); transaction tx = session.begintransaction(); // 对象 compositekeys keys = new compositekeys(); keys.setaddress(广州棠东); keys.setusername(jack); user user = new user(); user.setage(20); user.setkeys(keys); // 保存 session.save(user); tx.commit(); session.close(); } @test public void testget() throws exception { session session = sf.opensession(); transaction tx = session.begintransaction(); //构建主键再查询 compositekeys keys = new compositekeys(); keys.setaddress(广州棠东); keys.setusername(jack); // 主键查询 user user = (user) session.get(user.class, keys); // 测试输出 if (user != null){ system.out.println(user.getkeys().getusername()); system.out.println(user.getkeys().getaddress()); system.out.println(user.getage()); } tx.commit(); session.close(); }}
其它类似信息

推荐信息