Hibernate 配置文件

本文详细介绍了Hibernate配置文件hibernate.cfg.xml的作用及关键属性设置,包括JDBC连接信息配置、dialect属性选择、hmb2ddl.auto自动创建数据库语法等功能。同时,阐述了Java实体类的规范设计,如构造函数、getter/setter方法,以及如何在映射文件中进行属性映射。通过示例代码展示了如何获取SessionFactory并进行对象实例的保存操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

hibernate.cfg.xml文件定义了hibernate配置信息。


connection.driver_class,connection.url,connection.username 和 connection.password 的<property/>元素定义了JDBC连接信息。鉴于使用了内存数据库,所以这些属性的值都是专门在内存模式下运行。connection.pool_size 用于配置Hibernate的内置连接池的连接数量。

注意:hibernate内置的连接池还不能实际应用。其缺乏某些实际应用中的功能。


dialect 属性指定了Hibernate要使用哪种数据库 比如oracle  mysql 或 jdbc。


注意:在大多数情况,hibernate能够指定使用哪种数据库,这对于多数据库很有用。


hmb2ddl.auto属性能使数据库语法直接自动创建到数据库中。


最后,增加映射文件与持久类的配置。<mapping/>元素的resource属性使得hibernate可以使用java.lang.ClassLoader作为一个类路径资源定位映射查询。


有许多方法可以启动Hibernate 的SessionFactory。更多细节看下面的Bootstrapping。



2.2  Java 实体类

       这个教程中的实体类是org.hibernate.turtorial.hbm.Event

       

        这个实体类有get和set方法,也有私有的成员变量。虽然希望这样设计,但不是必须的。

        依照JavaBean的习惯,有一个无参的构造函数,这对于所有的持久类都是必须的。Hibernate使用Java反射机制创建对象。构造函数可以是私有的。但是,包或者公有对于运行代理机制和无字节机制的高效数据检索是必须的。


2.3 映射文件

     映射文件时org/hibernate/tutorial/hbm/Event.hbm.xml。


     Hibernate使用映射元数据决定如何加载和保存持久类的对象。

     例子:

     <class name="Event" table="EVENTS">

                   ...

    </class>

    name属性标示了全路径下的实体类

    table属性标示了这个类对应的数据库中的表。


Event类的实例现在映射到数据库EVENTS表的行中了。

例子2 id映射元素

    <id name="id" column="EVENT_ID">

           ...

    </id>


Hibernate 使用</id>标示这个实体类对应数据库中表的唯一性。


generator 元素指定Hibernate使用哪种策略创建这个实体类的唯一键值。这个例子使用了简单的数量递增。


例子3 属性映射元素

  <property name="date" type="timestamp" column="EVENT_DATE" />

  <property name="title"/>


上述两个property元素声明了这个Event类的两个持续性属性:date和title。date属性映射包含了列名属性,但是title没有。这里如果缺失列名,Hibernate使用属性名作为列名。这个适用于title,但是date在大多数据库中是关键字,需要指定一个新的列名以免冲突。

title映射也缺少type属性。在映射文件中类型声明和使用既不是Java的数据类型也不是SQL数据类型。而是Hibernate的自有映射类型,用于Java Bean类型和SQL类型之间转换。如果在映射中没有指定type类型,Hibernate自动决定正确转换和映射类型,通过使用Java反射机制决定声明属性的Java类型并使用默认的映射类型。


2.4 例子代码

  

     org.hibernate.tutorial.hbm.NativeApiIllustrationTest使用Hibernate本地API。

  

例子4 获取 org.hibernate.SessionFactory

  

protected void setUp() throws Exception{
	// a sessionFactory is set up once for an application
        final StandardServiceRegistry registry = new StandardServiceRegistryBuilder()
						 .configure()//configures setting from hibernate.cfg.xml
						 .bulid();
        try{
	     sessionFactory = new MetadataSources(registry).buildMetadata().buildSessionFactory();	
        } catch(Exception e){
             StandardServiceRegistryBuilder().destory(registry); 
        }  
}


</pre><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);">setUp方法首先创建了一个org.hibernate.boot.registry.StandardServiceRegistry 实例,通过sessionFactory将配置信息注入Services工作集中使用。在这里,配置信息全部定义在hibernate.cfg.xml。</span></p><p></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);">使用StandardServiceRegistry ,我们创建了org.hibernate.boot.MetadataSources,这是hibernate辨别领域类的起始点。这些我们都在hibernate.cfg.xml文件中配置过了。</span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);"></span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);">org.hibernate.boot.Metadata,SessionFactory基于此,表示这个应用领域模型完全地,部分地校验视图。</span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);"></span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);">bootstrap 处理的最后一步是建立SessionFactory。这个SessionFactory是线程安全的对象,在整个实体应用中只实例化一次。</span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);"></span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);">SessionFactory是创建org.hibernate.Session实例的工厂。</span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);"></span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);">例子5 保存实例</span></p><p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);"></span></p><span style="font-family:Courier New;font-size:14px;background-color: rgb(247, 247, 248);"></span><pre class="java" name="code">Session session = sessionFactory.openSession();
session.beginTransaction();
session.save(new Event("2343234",new Date()));
session.save(new Event("the second event",new Date()));
session.getTransaction().commit();
session.close()






 



   



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值