Hibernate programmable configuration method

Source: Internet
Author: User

Org. hibernate. cfg. ConfigurationAn instance represents an application.ProgramJava-SQL database ing complete set.Org. hibernate. cfg. ConfigurationIs used to build an (immutable ))Org. hibernate. sessionfactory. The ing definition is defined by different
The XML ing definition file is compiled.

You can instantiate it directly.Org. hibernate. cfg. ConfigurationTo get an instance and specify an XML ing definition file for it. If the ing definition file is in the classpath, useAddresource (). For example:

 Configuration  Cfg  =     New     Configuration ()    .  Addresource  (  "Item. HBM. xml"  )    .  Addresource  (  "Bid. HBM. xml" ); 

An alternative method (sometimes better) is to specify the ing class so that hibernate can help you find the ing definition file:

 Configuration  Cfg  =     New     Configuration  ()    . Addclass  (  Org  .  Hibernate  .  Auction  .  Item  .  Class  )   .  Addclass  (  Org  .  Hibernate  .  Auction  .  Bid  . Class  ); 

Hibernate will find the name in the classpath/Org/hibernate/auction/item. HBM. xmlAnd/Org/hibernate/auction/bid. HBM. xmlIng definition file. This method eliminates any hard encoding of the file name (hardcoded ).

Org. hibernate. cfg. Configuration> You can also specify configuration attributes. For example:

 Configuration  Cfg =     New     Configuration  ()    .  Addclass  (  Org  . Hibernate  .  Auction  .  Item  .  Class  )    .  Addclass  ( Org  .  Hibernate  .  Auction  .  Bid  .  Class  )    . Setproperty  (  "Hibernate. dialect"  ,     "Org. hibernate. dialect. mysqlinnodbdialect"  )    .  Setproperty  ( "Hibernate. Connection. datasource"  ,     "Java: COMP/ENV/jdbc/test"  )    .  Setproperty  (  "Hibernate. order_updates"  ,    "True"  ); 

Of course, this is not the only way to pass the hibernate Configuration Attribute. Other optional methods include:

  1. Upload oneJava. util. PropertiesInstanceConfiguration. setproperties ().

  2. set hibernate. properties is placed under the root directory (root directory) of the classpath ).

  3. using JAVA-dproperty = value to set the system ( System ) attribute.

  4. in hibernate. cfg. add the element

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.