Step 5: three common configuration methods.

Source: Internet
Author: User
There are three common configuration methods for configuring nhib.pdf.
1: Configure in Web. config and App. config
  <?  XML version = "1.0" encoding = "UTF-8"  ?>    <  Configuration  >    <! --  Add this element  -->    < Configsections  >    <  Section  Name  = "Hibernate-configuration"  Type  = "Nhibernate. cfg. configurationsectionhandler, nhibler"    />    </  Configsections  >   <! --  Add this element  -->    <  Hibernate-Configuration  Xmlns  = "Urn: nhibernate-configuration-2.2"  >    <  Session-factory  >    <  Property  Name = "Dialect"  >  Nhib.pdf. dialect. mssql2005dialect  </  Property  >    <  Property  Name  = "Connection. provider"  >  Nhib.pdf. Connection. driverconnectionprovider  </  Property  >   <  Property  Name  = "Connection. connection_string"  >  Server = tlsz207 \ sqlexpress; initial catalog = test; Integrated Security = true  </  Property  >    </  Session-factory  >    </ Hibernate-Configuration  >    <! --  Leave the system. web section unchanged  -->    <  System  . Web  >    </  System. Web  >    </ Configuration  > 

The configuration object needs to be instantiated in this way.
Nhibernate. cfg. Configuration CFG = new Nhibernate. cfg. configuration ();
This configuration method will be appliedProgramIn the configuration file (App. config, Web. config), find the configuration information of Nhibernate.

2: hibernate. cfg. xml
Create a file named hibernate. cfg. xml. Instantiate configuration Config = new configuration (). Configure (); in this way, nhib.pdf searches for the configuration file of hibernate. cfg. XML in the directory.
Hibernate. cfg. XML format

  <?  XML version = "1.0" encoding = "UTF-8"  ?>    < Hibernate-Configuration  Xmlns  = "Urn: nhibernate-configuration-2.0"     >    <  Session-factory  Name  = "Mysessionfactory"  >    <  Property  Name  = "Dialect"  > Nhib.pdf. dialect. mssql2005dialect  </  Property  >    <  Property  Name  = "Connection. provider"  >  Nhib.pdf. Connection. driverconnectionprovider  </  Property  >    < Property  Name  = "Connection. connection_string"  >  Server = tlsz207 \ sqlexpress; initial catalog = test; Integrated Security = true  </  Property  >    </  Session-factory  >    </  Hibernate-Configuration  > 

Specify the configuration file
Configuration Config = new configuration (). Configure (configfilename );
This configuration method searches for the specified hibernate standard configuration file, which can be an absolute or relative path. You can also add configuration information by encoding:
Idictionary props = new hashtable ();
Props ["dialect"] = "nhib.pdf. dialect. mssql2005dialect ";
...
Configuration CFG = new configuration ();
Cfg. properties = props; // cfg. addproperties (props );

Ing file:
All XML mappings require the use of nhibernate-mapping-2.0 schema. The current schema can be found in the resource path of nhib.pdf or the embedded Resource (embedded Resource) of nhib.pdf. dll. Nhib.pdf always preferentially uses schema files embedded in resources. You can copy hibernate-mapping to the path c: \ Program Files \ Microsoft Visual Studio. NET 2003 \ common7 \ packages \ schemas \ XML for smart sensing.

  <?  XML version = "1.0" encoding = "UTF-8" ?>    <  Hibernate-Mapping  Xmlns  = "Urn: nhibernate-mapping-2.2"  Namespace  = "Test"  Assembly  = "Test"  >    <  Class  Name  = "Test. Cat, test"  Table = "Cat"  >    <  ID  Name  = "Catid"  >    <  Column  Name  = "Catid"  SQL-type  = "Char (32 )"  Not-Null  = "True"  />   <  Generator  Class  = "UUID. Hex"     />    </  ID  >    <  Property  Name  = "Name"  >   <  Column  Name  = "Name"  Length  = "16"  Not-Null  = "True"     />    </  Property  >    <  Property  Name = "Sex"     />    <  Property  Name  = "Weight"     />    </  Class  >    </  Hibernate-Mapping  > 

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.