Hibernate配置文件的示例分析

发布时间:2021-11-17 17:54:45 作者:小新
来源:亿速云 阅读:134

小编给大家分享一下Hibernate配置文件的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

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

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

那么初始化的代码大致为

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

如果Hibernate配置文件为XML,则

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 

以上是“Hibernate配置文件的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

推荐阅读:
  1. hibernate环境搭建测试的示例分析
  2. hibernate中配置文件工作原理的示例分析

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

hibernate

上一篇:如何理解DIV中display和visibility属性差别

下一篇:jquery如何获取tr里面有几个td

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》