浅析Hibernate配置文件

开发 后端
这里介绍有两种Hibernate配置文件格式,一种是XML,默认为Hibernate.cfg.xml,一种是properties,默认为Hibernate.properties不同的配置文件,对Hibernate的初始化方法是不一样的。

Hibernate有很多值得学习的地方,这里我们主要介绍Hibernate配置文件,包括介绍XML文件格式等方面。

有两种Hibernate配置文件格式,一种是XML,默认为Hibernate.cfg.xml,一种是properties,默认为Hibernate.properties不同的配置文件,对Hibernate的初始化方法是不一样的。

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

那么初始化的代码大致为

  1. Configuration config = new Configuration();  
  2. config.addClass(myclass.class); 

如果Hibernate配置文件为XML,则

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

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

另:网上好多文章,甚至有的书都说,Hibernate的配置文件必须放在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 

【编辑推荐】

  1. Hibernate可行性全面概括
  2. Hibernate Sequence基础描述
  3. 浅析Hibernate使用EhCache
  4. Hibernate的一级Cache
  5. Hibernate update浅谈
责任编辑:佚名 来源: Sina
相关推荐

2009-09-25 10:49:25

Hibernate加载

2009-09-27 13:25:22

2009-09-29 15:52:26

Hibernate X

2009-09-29 17:29:43

Hibernate S

2009-08-13 09:16:57

C#读取配置文件

2012-02-06 13:34:49

HibernateJava

2009-07-21 10:05:10

ASP.NET配置文件

2009-09-24 10:07:21

Hibernate M

2009-08-05 10:57:17

ASP.NET配置文件配置文件格式

2009-09-22 14:44:18

Hibernate.c

2009-06-23 18:19:32

单元测试Hibernate配置

2009-06-02 14:12:26

Hibernate配置文件格式

2009-08-05 11:16:26

ASP.NET配置文件

2009-09-28 17:23:51

Hibernate E

2011-01-19 14:00:21

2009-09-21 18:13:11

Hibernate S

2009-09-23 10:19:08

Hibernate分页

2009-09-23 17:28:55

Hibernate C

2009-09-21 12:50:34

Hibernate架构

2009-09-24 11:41:46

Hibernate延迟
点赞
收藏

51CTO技术栈公众号