About the configuration in hibernate

Source: Internet
Author: User

In hibernate, there are two ways to build a sessionfactory from the configuration that can be used One is to provide a Hibernate.cfg.xml profile for the configuration, and the other is to provide a hibernate.properties configuration file.

1. Provide the Hibernate.cfg.xml configuration file:

static {
Configuration Config=null;
try {
Config = new Configuration (). Configure ();//using Configure () method to load the default Hibernate.cfg.xml configuration file because the configuration file already contains database mapping information, so you do not need to use the AddClass method to load the mapping files for each class file
} catch (Hibernateexception E1) {
E1.printstacktrace ();
}
try {
Sessionfactory=config.buildsessionfactory ();
} catch (Exception e) {
E.printstacktrace ();
}
}

2. Provide the hibernate.properties configuration file:

static {
Configuration Config=null;
Config = new Configuration ();//Because the new one configuartion loads the Hibernate.proprties file by default, you do not need to call the Configure method.
try {
Config.addclass (Manager.class);//Because there is no mapping information in the Hibernate.properties file, you need to call the AddClass method to explicitly load the mapping file for the class file
Sessionfactory=config.buildsessionfactory ();
} catch (Exception e) {
E.printstacktrace ();
}
}

If the method uses error, error message will be various, I in this problem after two weeks of trouble finally solved!!!

About the configuration in hibernate

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.