所在位置:北大青鸟广州天河培训中心 >> 漫谈软件技术流 >>不同配置文件的初始化Hibernate心得

不同配置文件的初始化Hibernate心得

Hibernate有两种配置文件格式,一种是XML,默认为hibernate.cfg.xml,一种是properties,默认为hibernate.properties

不同的配置文件,对Hibernate的初始化方法是不一样的

比如,如果采用properties文件作为配置文件

那么初始化的代码大致为

Configuration config = new Configuration();

config.addClass(myclass.class);

如果配置文件为XML,则

Configuration config = new Configuration().config();

    XML文件格式的配置文件不支持addClass方法!!!这是因为在配置文件XML文件中,已经定义了Mpaaing文件,因此就不需要在用编码方式导入POJO文件了。

    另:网上好多文章,甚至有的书都说,Hibenate的配置文件必须放在class的根目录,参考一下API,发现这个说法是不正确的,例如Configuration config = new Configuration().config(配置文件名);完全可以的。

所有config方法如下:

1  addCacheableFile(File xmlFile)

    If a cached xmlFile + ".bin" exists and is newer than xmlFile the ".bin" file will be read directly.

2  Configuration addClass(Class persistentClass)
          Read a mapping from an application resource, using a convention.

3  Configuration addDirectory(File dir)
          Read all mapping documents from a directory tree.

4  Configuration addDocument(org.w3c.dom.Document doc)
          Read mappings from a DOM Document

5  Configuration addFile(File xmlFile)
          Read mappings from a particular XML file

6  Configuration addFile(String xmlFile)
          Read mappings from a particular XML file

7  void addFilterDefinition(FilterDefinition definition)

8  Configuration addInputStream(InputStream xmlInputStream)
          Read mappings from an InputStream

9  Configuration addJar(File jar)
          Read all mappings from a jar file

10  Configuration addProperties(Properties extraProperties)
          Set the given properties

11  Configuration addResource(String path)
          Read mappings from an application resource trying different classloaders.

12  Configuration addResource(String path, ClassLoader classLoader)
          Read mappings from an application resource

13  Configuration addURL(URL url)
          Read mappings from a URL

14  Configuration addXML(String xml)
          Read mappings from a String

下一篇:使用AJAX技术构建更优秀的Web应用程序

开班信息
咨询热线:020-85566215
名师讲座

软件世界 梦幻体验

讲座时间:周六、日 14:00
1.软件行业真的那么高深莫测?
2.软件技术真的那么难学?
3.学习软件能做什么工作?
4.软件行业可否托付终身?

 让北大青鸟金牌培训师为你
  逐一打开问号!!

订座热线:020-85566216