Hibernate-Configuration:配置对象

来源:互联网 发布:淘宝购物的金钱过程 编辑:程序博客网 时间:2024/06/06 04:55

Configuration 配置对象


        An instance of Configuration allows the application to specify properties and mapping documents to be used when creating a SessionFactory. Usually an application will create a single Configuration, build a single instance of SessionFactory and then instantiate Sessions in threads servicing client requests. The Configuration is meant only as an initialization-time object. SessionFactorys are immutable and do not retain any association back to the Configuration.

        Configuration 实例允许应用程序指定在创建SessionFactory时使用的属性和映射文档。通常,应用程序将创建单个配置,构建SessionFactory的单个实例,然后在服务客户端请求的线程中实例化会话。配置仅作为初始化时间对象。SessionFactorys是不可变的,并且不保留任何关联到配置。

    说白了,Configuration 对象主要用于对Hibernate框架加载映射文件,在Hibernate启动的过程中,Configuration 对象的实例首先定位映射文档的位置,然后读取这些配置,创建一个SessionFactory对象,Configuration 对象是Hibernate在启动时遇到的第一个对象。

    Hibernate默认加载src目录下的hibernate.cfg.xml配置文件,如果不想使用默认的hibernate.cfg.xml配置文件,而是使用指定目录下的配置文件,需要向configure()方法传递一个文件路径参数:

Configuration config = new Configuration().configure("xml文件位置");

    如果想使用src下config文件夹内的hibernate.cfg.xml文件,只需要将文件位置加入configure()中即可

Configuration config = new Configuration().configure("/config/hibernate.cfg.xml");

    Hibernate除了可以使用Configuration对象加载核心配置文件外,还可以使用该对象加载映射文件,这样就可以使用properties文件作为hibernate的核心配置文件,其他属性可以使用key=value的格式来设置。

Configuration configuration = new Configuration.configure("xml文件位置");configuration.addResource("实体类.hbm.xml")
原创粉丝点击