Hibernate参考文档学习笔记——Hibernate入门(四)

每个人都有潜在的能量,只是很容易被习惯所掩盖,被时间所迷离,被惰性所消磨。


今天的学习任务就是Hibernate的配置,由于Hibernate要屏蔽数据库之间的差异,因此存在着众多的配置参数。

1、      可编程的配置方式

  org.hibernate.cfg.Configuration表示应用程序中实体类型到数据库的完整集合,是Hibernate中用于创建org.hibernate.SessionFactory的。

  可以采用不同的方式添加映射文件,直接指定映射文件,或指定映射的类。

    1.1         指定映射文件

1 Configuration cfg = new Configuration()
2 
3                             .addResource("Item.hbm.xml")
4 
5                             .addResource("Bid.hbm.xml");
View Code

    注意:映射文件必须在类路径中

    1.2         指定映射的类

1 Configuration cfg = new Configuration()
2 
3                             .addClass(org.hibernate.auction.Item.class)
4 
5                             .addClass(org.hibernate.auction.Bid.class);
View Code

         这样配置之后Hibernate会在类路径中寻找“/org/hibernate/auction/Item.hbm.xml”和“/org/hibernate/auction/Bid.hbm.xml”

     也可以通过编码方式配置属性,只需要使用setProperty()方法。

1 Configuration cfg = new Configuration()
2                            .addClass(org.hibernate.auction.Item.class)
3                            .addClass(org.hibernate.auction.Bid.class)
4                            .setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLInnoDBDialect");
View Code

2、      获得SessionFactory

  将映射文件都加入到org.hibernate.cfg.Configuration之后,就需要得到一个SessionFactory来创建org.hibernate.Session。

1 SessionFactory factory = cfg.buildSessionFactory();
View Code

注意:buildSessionFactory()方法以及被buildSessionFactory(ServiceRegistry)方法代替了。

Hibernate允许你的应用程序创建多个 org.hibernate.SessionFactory 实例。这是为了应对使用多个数据库的应用的。

3、      JDBC连接

  有了SessionFactory之后,就可以创建 org.hibernate.Session了,

1 Session session = factory.openSession();
View Code

这样,一旦需要进行数据访问,就会从连接池中获取一个JDBC连接了。

  为了使这种方式工作起来,我们需要向 Hibernate 传递一些 JDBC 连接的属性。所有 Hibernate
属性的名字和语义都在 org.hibernate.cfg.Environment 中定义。

4、      XML 配置文件

也可以使用配置文件来完成编程配置方式所完成的工作,hibernate.cfg.xml就是用来做这个工作的,该文件默认放在类路径的根目录下。使用这种方式比使用编程配置方式方便的多,也更加利于修改,所以这也是推荐的方式。

 1 <?xml version='1.0' encoding='utf-8'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3 "-//Hibernate/Hibernate Configuration DTD//EN"
 4 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
 5 <hibernate-configuration>
 6 <!-- a SessionFactory instance listed as /jndi/name -->
 7 <session-factory
 8 name="java:hibernate/SessionFactory">
 9 <!-- properties -->
10 <property name="connection.datasource">java:/comp/env/jdbc/MyDB</property>
11 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
12 <property name="show_sql">false</property>
13 <property name="transaction.factory_class">
14 org.hibernate.transaction.JTATransactionFactory
15 </property>
16 <property name="jta.UserTransaction">java:comp/UserTransaction</property>
17 <!-- mapping files -->
18 <mapping resource="org/hibernate/auction/Item.hbm.xml"/>
19 <mapping resource="org/hibernate/auction/Bid.hbm.xml"/>
20 <!-- cache settings -->
21 <class-cache class="org.hibernate.auction.Item" usage="read-write"/>
22 <class-cache class="org.hibernate.auction.Bid" usage="read-only"/>
23 <collection-cache collection="org.hibernate.auction.Item.bids" usage="read-write"/>
24 </session-factory>
25 </hibernate-configuration>
View Code
原文地址:https://www.cnblogs.com/forgive/p/3417664.html