`

Hibernate基础知识点总结—第一部分

 
阅读更多



 Hibernate3.6
持久层的框架

 

添加环境:
 1,jar包
 2,配置文件
  hibernate.cfg.xml
  xxx.hbm.xml
 
使用Hibernate实现CRUD操作
 // --- 准备
 Configuration cfg = new Configuration().configure(); // hibernate.cfg.xml
 SessionFactory sessionFactory = cfg.buildSessionFactory(); // 只需要一个

 // --- 模板代码
 Session session = sessionFactory.openSession();
 Transaction tx = null;
 try{
  tx = session.beginTransaction();
  // 操作
  tx.commit();
 }catch(Exception e){
  tx.rollback();
  throw e;
 }finally{
  session.close();
 }

 // --- 操作
 Session中的方法:
  save(Object)  --> insert into ..
  update(Object)  --> update ..
  saveOrUpdate(Object)
  delete(Object)  --> delete ..
  get(Class, id)  --> select ...
  createQuery(hql) --> select ..


主配置文件
 1,数据库信息
  方言、URL、驱动、用户名、密码
 2,导入映射文件
 3,其他配置
  show_sql = true
  hbm2ddl.auto = update
 
映射配置:
 映射基础
  类 -- 表
  属性 -- 列
 映射普通属性
  name, type, column, length, not-null, ...
 映射主键
  主键生成略:native, uuid

 

 

Session


SQL
 查询的是表和表中的字段。
 不区分大小写

HQL
 Hibernate Query Language
 与SQL相似
 查询的是对象和对象中的属性。
 关键字不区分大小写,但类名与属性名区分大小写。


==========================================================


API
 1,API简介。
 2,Session中的方法。
 3,查询:HQL与Criteria

配置:
 1,主配置文件
 2,映射文件
  映射基础
  普通属性
  主键
  集合属性
  关联关系
   一对多/多对一
   多对多
   一对一
  继承结构


-----------------------------------
-- API简介

Configuration 配置
 configure() 
 configure(String resource)
 addResource(String resource) 导入一个指定位置的映射文件
 addClass(Class clazz)   导入与指定类同一个包中的以类名为前缀,后缀为.hbm.xml的映射文件
 buildSessionFactory()

SessionFactory Session工厂
 openSession()
 getCurrentSession()
 close()

Session   很重要的一个对象
 操作对象的方法
 save(Object)
 update(Object)
 delete(Object)
 查询的方法
 createQuery(String) --> Query
 createCriteria(Class)
 管理事务的方法
 beginTransaction() --> Transaction
 getTransaction()   --> Transaction 获取当前Session中关联的事务对象
 其他的方法
 ...

Transaction  事务
 commit()
 rollback()

Query   查询
 list()   查询一个结果集合。
 uniqueResult() 查询一个唯一的结果,如果没有结果,则返回null,如果结果有多个,就抛异常。
 ...


--------------------------------

Hibernate主配置文件

1,配置的key前面的hibernate.前缀 可以有,也可以没有。如hibernate.dialect或dialect都可以。
2,按作用可分为三类:
 1,数据库信息
  <property ...>
  方言、JdbcUrl、驱动、用户名、密码
 2,导入映射文件
  <mapping ...>
 3,其他配置
  <property ...>
  show_sql  显示生成的SQL语句
  format_sql  格式化生成的SQL语句
  hbm2ddl.auto 自动生成表结构
  hibernate.hbm2ddl.auto
生成表结构的两种方式:
 1,hbm2ddl.auto
 2,使用SchemaExport工具类
注意:只能建表,不能建库


==============
主键:
 如果是数字,建议使用包装类型。

identity
sequence
hilo
native
assigned
uuid
foreign
...

  • 大小: 33.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics