Hibernate初始化经验总结
创始人
2024-06-08 04:11:06
0

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核心接口

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...