Get the App
SLTechnology News&Howtos  ›  Development  › 

How to initialize Hibernate

Shulou Source: shulou.com Published: 2022-06-02 09:15:10 09月17日 Update

Xiaobian to share with you how Hibernate initialization, I believe most people do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!

Hibernate has two configuration file formats, XML (hibernate.cfg.xml by default) and properties (Hibernate. properties by default). Different configuration files have different initialization methods for Hibernate

For example, if the properties file is used as a configuration file, then Hibernate initialization code is roughly

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

If the configuration file is XML,

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

Configuration files in XML file format do not support addClass method!!! This is because the Mpaaing file is already defined in the configuration XML file, so there is no need to import the POJO file encoded.

Another: many articles on the Internet, and even some books say that Hibenate configuration files must be placed in the root directory of the class, refer to the API, found that this statement is incorrect, such as Configuration config = new Configuration().config(configuration file name); completely OK.

All config methods are as follows:

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(org.w3c.dom.Document 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

That's all for Hibernate initialization, thanks for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!

Tags: File configuration method article content format reference different not much code most file name article method more root directory knowledge coding industry statement Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Tech Info Redmi Docker MariaDB vpn