Hibernate总结2 API和配置文件

1,Configuration 配置

获取config配置文件的方法

Configuration cfg = new Configuration();

cfg.下面的方法

    • configure()
    • configure(String resource)

添加映射文件,映射文件为类名.hbm.xml配置文件,用于配置类的成员变量和表列的相匹配关系

addResource(String resource) 导入一个指定位置的映射文件

载入类,根据类.Class来载入类名

addClass(Class clazz) 导入与指定类同一个包中的以类名为前缀,后缀为.hbm.xml的映射文件

生成sessionFacotry

buildSessionFactory()

2,SessionFactory Session工厂

获取Session,关闭Session,此关闭session是指将session返还给session工厂

openSession()
getCurrentSession()
close()

3,Session 很重要的一个对象

操作对象的方法

    • save(Object)
    • update(Object)
    • delete(Object)

查询的方法

    • createQuery(String) --> Query
    • createCriteria(Class)

管理事务的方法

    • beginTransaction() --> Transaction
    • getTransaction() --> Transaction 获取当前Session中关联的事务对象

其他的方法
...

Transaction 事务

    • commit()
    • rollback()

Query 查询

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

4,Hibernate主配置文件

1,数据库信息

<property ...>

方言、JdbcUrl、驱动、用户名、密码

2,导入映射文件

<mapping ...>

3,其他配置

<property ...>

show_sql 显示生成的SQL语句

format_sql 格式化生成的SQL语句

hbm2ddl.auto 自动生成表结构

hibernate.hbm2ddl.auto  自动生成表结构

5,主键类型

如果主键是数字的话,类建议使用包装类型,比如说Integer类型来代替int类型

表中的主键常用<generator class="native" />来作为主键的生成规则,因为这个匹配大部分数据库,包括有自动增长(mssql,mysql)的和无自动增长通过序列的(oracle)

6,插入集合类型的

常见的集合有Set(hashset),list(arraylist),array,map(hashmap)等,在xxx.hbm.xml配置时,具体的配置如下

 1 <!-- addressSet属性,Set集合 
 2             table属性:集合表的名称
 3             key子元素:集合外键的列名
 4             element子元素:存放集合元素的列的信息
 5             sort属性:"unsorted|natural|comparatorClass"
 6                 默认为:unsorted
 7             order-by属性:写的是order by 子句,是SQL语句,是操作的集合表。
 8                 这是在查询数据时指定orderby子句。
 9         -->
10         <set name="addressSet" table="user_addressSet" order-by="address ASC">
11             <key column="userId"></key>
12             <element type="string" column="address"></element>
13         </set>
14         
15         <!-- addressList属性,List集合 
16             list-index:用于存放索引的列
17         -->
18         <list name="addressList" table="user_addressList">
19             <key column="userId"></key>
20             <list-index column="idx"></list-index>
21             <element type="string" column="address"></element>
22         </list>
23         
24         <!-- addressArray属性,数组。与List的映射基本一致 -->
25         <array name="addressArray" table="user_addressArray">
26             <key column="userId"></key>
27             <list-index column="idx"></list-index>
28             <element type="string" column="address"></element>
29         </array>
30         
31         <!-- addressMap属性,Map集合 -->
32         <map name="addressMap" table="user_addressMap">
33             <key column="userId"></key>
34             <map-key type="string" column="key_"></map-key>
35             <element type="string" column="address"></element>
36         </map>
37         
38         <!-- addressBag属性,Bag集合:无序,可重复。与Set集合的映射基本一致 -->
39         <bag name="addressBag" table="user_addressBag">
40             <key column="userId"></key>
41             <element type="string" column="address"></element>
42         </bag>
hbm.xml集合配置

注意:使用集合属性时,一定要使用接口,而不能声明为具体的实现类。因为经过Session操作后,集合就变成了Hibernate自己的集合实现类。

7,映射关联关系

多对一关系

1         <!-- department属性,表达的是本类与Department的多对一 
2             class属性:关联的实体类型
3             column属性:外键列(引用关联对象的表的主键)
4         -->
5         <many-to-one name="department" class="Department" column="departmentId"></many-to-one>
多对一关系,存在于hbm.xml文件中

一对多关系

 1         <!-- employees属性,Set集合,表达的是本类与Employee的一对多 
 2             class属性:关联的实体类型
 3             key子元素:对方表中的外键列(多方的那个表)
 4             
 5             inverse属性:
 6                 默认为false,表示本方维护关联关系。
 7                 如果为true,表示本方不维护关联关系。
 8                 只是影响是否能设置外键列的值(设成有效值或是null值),对获取信息没有影响。
 9                 
10             cascade属性:
11                 默认为none,代表不级联。
12                 级联是指操作主对象时,对关联的对象也做相同的操作。
13                 可设为:delete, save-update, all, none ...
14         <set name="employees" cascade="all">
15             <key column="departmentId"></key>
16             <one-to-many class="Employee"/>
17         </set>
一对多,存在于hbm.xml文件中

   多对多关系

1         <!-- name对应于对象中多的那个集合的属性值 ,table对饮两个对对多的表名,inverse对应是否相互通知对方-->
2         <set name="teachers" table="teacher_student" inverse="true">
3         <!-- key对应于对象在对应关系表中的主键 -->
4             <key column="stu_id"></key>
5             <!-- class对应与多对多另外一方的类名,column对应在多对多表中,另外一个类的主键对应的值 -->
6             <many-to-many class="Teacher" column="te_id"></many-to-many>
7         </set>
多对多关系,存在于hbm.xml文件中

 8,映射关系简单图鉴

一对多(Set)
<Set name="">
<key column=""/> (写对方类的表达此关系的外键列名)
<one-to-many class=""/>
</Set>

多对一
<many-to-one name="" class="" column="" />

多对多<set>
<set name="" talbe="">
<key column=""/> (引用自己主键)
<many-to-many class="" column/>(引用对方主键)
</set>

一对一(基于外键的有外键方)
<many-to-one name="" class="" unique="true" />

一对一(基于外键的无外键方)
<one-to-one name="" class="" property-ref=""/>

原文地址:https://www.cnblogs.com/ningheshutong/p/5673667.html