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 >