5--Hibernate basic usage--4 1 Creating a Configuration object

Source: Internet
Author: User

The Org.hibernate.cfg.Configuration instance represents the configuration information for the application to the SQL database, which provides a buildsessionfactory () method. This method can produce an immutable Sessionfactory object.

Also, instantiate the configuration instance first, and then add the Hiberante persistence class. The configuration object can call the Addannotatedclass () method to add a persisted class one by one, or you can call the Addpackage () method to add all persisted classes under the specified package.

The way you create a configuration object differs depending on the hibernate configuration file:

⊙ use hibernate.properties file as configuration file

⊙ use Hibernate.cfg.xml file as configuration file

⊙ create a configuration object by encoding without using any profile

The only purpose of a configuration instance is to create an sessionfactory instance, so it is designed to be a startup object, and once the sessionfactory is created, it is discarded.

1. Using hibernate.properties as the configuration file

Under the project\etc path of the hibernate release package, a hibernate.properties file is provided that loves you to list all the properties of the Hibernate configuration file in detail.

Because Hibernate.properties does not provide a way to add hibernate persistence classes as a configuration file, you must call the Addannotatedclass () or Addpackage () method of the Configuration object. Use these methods to add a persisted class.

New Configuration (). Addannotatedclass (person.  Class). Addannotatedclass (Student.  Class);

2. Using Hibernate.cfg.xml as the configuration file

You can use the Hibernate.cfg.xml configuration file to add Hibernate persistence classes through <mapping.../> child elements, so you do not need to add persisted classes programmatically.

        // the Configure () method will be responsible for loading the Hibernate.cfg.xml file        New Configuration (). Configure ();

3. Create a configuration instance without using a config file

The configuration object provides the following methods for creating a configuration Instance programmatically:

⊙configuration Addannotatedclass (class Annotatedclass): Used to add a persisted class to a Configuration object.

⊙configuration addpackage (String packagename): Used to add all persisted classes under the specified package for the Configuration object.

⊙configuration SetProperties (properties): Used to set a series of properties for a Configuration object, a series of properties passed in through a properties instance.

⊙configuration setProperty (String propertyname,string value): Used to set a separate property for a Configuration object.

Class:newsmanager

 PackageHibernate.book._5_4_1._3;Importorg.hibernate.Session;Importorg.hibernate.SessionFactory;Importorg.hibernate.Transaction;Importorg.hibernate.cfg.Configuration; Public classNewsmanager { Public Static voidMain (string[] args) {Configuration conf=NewConfiguration (). Addannotatedclass (News.class). SetProperty ("Hibernate.connection.driver_class", "Com.mysql.jdbc.Driver"). SetProperty ("Hibernate.connection.url", "Jdbc:mysql:///hibernate"). SetProperty ("Hibernate.connection.username", "root"). SetProperty ("Hibernate.connection.password", "System"). SetProperty ("Hibernate.c3p0.max_size", "20"). SetProperty ("Hibernate.c3p0.min_size", "1"). SetProperty ("Hibernate.c3p0.timeout", "5000"). SetProperty ("Hibernate.c3p0.max_statements", "100"). SetProperty ("Hibernate.c3p0.idle_test_period", "3000"). SetProperty ("Hibernate.c3p0.acquire_increment", "2"). SetProperty ("Hibernate.c3p0.validate", "true"). SetProperty ("Hibernate.dialect", "Org.hibernate.dialect.MySQL5InnoDBDialect"). SetProperty ("Hibernate.hbm2ddl.auto", "Update"); Sessionfactory sessionfactory=conf.buildsessionfactory (); Session Session=sessionfactory.opensession (); Transaction TX=session.begintransaction (); News News=NewNews (); News.settitle ("Angel"); News.setcontent ("Spend all your time waiting for that second chance");                Session.save (news);                Tx.commit ();                Session.close ();    Sessionfactory.close (); }}

Suitable for adding some of the key configuration properties in your code.

La La la

5--Hibernate basic usage--4 1 Creating a Configuration object

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.