Hibernate初始化经验总结

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

Hibernate还是比较常用的,于是我研究了一下Hibernate初始化,在这里拿出来和大家分享一下,希望对大家有用。

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

比如,如果采用properties文件作为配置文件,那么Hibernate初始化的代码大致为

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

如果配置文件为XML,则

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

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

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

所有config方法如下:

  1. addCacheableFile(File xmlFile)  
  2.  
  3. If a cached xmlFile + ".bin" exists and is newer than xmlFile the ".
    bin" file will be read directly.  
  4.  
  5. Configuration addClass(Class persistentClass)  
  6. Read a mapping from an application resource, using a convention.  
  7.  
  8. Configuration addDirectory(File dir)  
  9. Read all mapping documents from a directory tree.  
  10.  
  11. Configuration addDocument(org.w3c.dom.Document doc)  
  12. Read mappings from a DOM Document  
  13.  
  14. Configuration addFile(File xmlFile)  
  15. Read mappings from a particular XML file  
  16.  
  17. Configuration addFile(String xmlFile)  
  18. Read mappings from a particular XML file  
  19.  
  20. void addFilterDefinition(FilterDefinition definition)  
  21.  
  22. Configuration addInputStream(InputStream xmlInputStream)  
  23. Read mappings from an InputStream  
  24.  
  25. Configuration addJar(File jar)  
  26. Read all mappings from a jar file  
  27.  
  28. Configuration addProperties(Properties extraProperties)  
  29. Set the given properties  
  30.  
  31. Configuration addResource(String path)  
  32. Read mappings from an application resource trying different classloaders.  
  33.  
  34. Configuration addResource(String path, ClassLoader classLoader)  
  35. Read mappings from an application resource  
  36.  
  37. Configuration addURL(URL url)  
  38. Read mappings from a URL  
  39.  
  40. Configuration addXML(String xml)  
  41. Read mappings from a String  

以上为笔者刚接触Hibernate初始化的一个总结,不正确的地方还请各位多多指教!!

【编辑推荐】

  1. 简单描述Hibernate单元测试
  2. 概括Hibernate多表关联查询
  3. 浅谈Hibernate Session Factory
  4. 浅析Hibernate加载配置文件
  5. 深入剖析Hibernate核心接口
责任编辑:佚名 来源: 51CTO.com
相关推荐

2009-09-29 16:32:11

OJB Hiberna

2009-09-25 17:26:55

使用Hibernate

2009-09-27 14:53:38

Hibernate S

2009-10-15 09:27:00

2009-09-16 17:13:54

学习Linq

2009-08-19 09:24:43

AJAX引擎经验总结

2009-11-17 11:24:00

PHP应用技巧

2009-09-11 13:29:31

LINQ查询操作

2009-08-20 17:35:47

Servlet和JSP

2009-09-16 17:44:54

LINQ to SQL

2010-06-12 17:37:18

UML实践指南

2009-09-08 10:57:55

LINQ查询操作

2011-07-08 13:15:52

JSP

2009-10-22 15:07:12

综合布线工程

2009-09-08 16:02:47

Linq使用Group

2013-12-18 15:54:21

2010-01-26 13:28:11

Android开发要点

2011-07-21 13:40:17

java

2010-07-16 09:14:49

Perl模式

2009-08-13 18:13:27

C#学习经验
点赞
收藏

51CTO技术栈公众号