熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Hibernate的兩種配置文件格式

2022-06-13   來源: Java開源技術 

  Hibernate有兩種配置文件格式一種是XML默認為hibernatecfgxml一種是properties默認為hibernateproperties不同的配置文件Hibernate有兩種配置文件格式一種是XML默認為hibernatecfgxml一種是properties默認為hibernateproperties

  不同的配置文件對Hibernate的初始化方法是不一樣的

  比如如果采用properties文件作為配置文件

  那麼初始化的代碼大致為

  Configuration config = new Configuration();

  configaddClass(myclassclass);


    如果配置文件為XML

  Configuration config = new Configuration(nfig();

  XML文件格式的配置文件不支持addClass方法!!!這是因為在配置文件XML文件中已經定義了Mpaaing文件因此就不需要在用編碼方式導入POJO文件了

  另網上好多文章甚至有的書都說Hibenate的配置文件必須放在class的根目錄參考一下API發現這個說法是不正確的例如Configuration config = new Configuration()config(配置文件名)完全可以的

  所有config方法如下

   addCacheableFile(File xmlFile)

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

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

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

   Configuration addDocument(orgwcdomDocument doc)
Read mappings from a DOM Document

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

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

   void addFilterDefinition(FilterDefinition definition)

   Configuration addInputStream(InputStream xmlInputStream)
Read mappings from an InputStream

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

   Configuration addProperties(Properties extraProperties)
Set the given properties

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

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

   Configuration addURL(URL url)
Read mappings from a URL

   Configuration addXML(String xml)
Read mappings from a String


From:http://tw.wingwit.com/Article/program/Java/ky/201311/27883.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.