Hibernate的兩種設定檔格式

來源:互聯網
上載者:User

Hibernate有兩種設定檔格式,一種是XML,預設為hibernate.cfg.xml,一種是properties,預設為hibernate.properties不同的設定檔,Hibernate有兩種設定檔格式,一種是XML,預設為hibernate.cfg.xml,一種是properties,預設為hibernate.properties

不同的設定檔,對Hibernate的初始化方法是不一樣的

比如,如果採用properties檔案作為設定檔

那麼初始化的代碼大致為

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

如果設定檔為XML,則

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

XML檔案格式的設定檔不支援addClass方法!!!這是因為在設定檔XML檔案中,已經定義了Mpaaing檔案,因此就不需要在用編碼方式匯入POJO檔案了。

另:網上好多文章,甚至有的書都說,Hibenate的設定檔必須放在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 resource13 Configuration addURL(URL url) Read mappings from a URL 14 Configuration addXML(String xml) Read mappings from a String

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.