NHibernate Basic configuration (first article)

Source: Internet
Author: User

The most important step in using NHibernate is to configure, if even nhibernate have not run up, talk about what to learn. Let's take a look at the configuration of NHibernate today.

First, nhibernate basic configuration

NHibernate configuration to be aware of:

1, NHibernate need a custom configuration node, usually placed in Web. config or app. Config, of course you can define the actual location.

Examples are as follows:

<?XML version= "1.0" encoding= "Utf-8"?><Configuration><configsections><SectionName= "Hibernate-configuration"Type= "Nhibernate.cfg.configurationsectionhandler,nhibernate"/>(1)</configsections><Hibernate-configurationxmlns= "urn:nhibernate-configuration-2.2"><Session-factoryName= "Dao"><PropertyName= "Connection.driver_class">nhibernate.driver.sqlclientdriver</Property>(2)<PropertyName= "Connection.connection_string">Data source=kissdodog-pc;initial catalog=test;uid=sa;pwd=123; (3)</Property><PropertyName= "dialect">nhibernate.dialect.mssql2008dialect</property>  (4) <mapping assembly  = "Model" />  (5) </session-factory > </hibernate-configuration></configuration >             

(1), declaring a custom configuration node

Name: The Custom configuration node names.

Type: handler location.

(2), which database driver is declared.

(3), database connection.

(4), the SQL dialect used, through this dialect, NHibernate know to write your query statement, translated into what kind of database SQL statement.

(5), set the assembly where the mapping file is located by default. ‘

NHibernate Configuration node more than 5, want to know more detailed configuration node Description:http://www.cnblogs.com/kissdodog/archive/2013/02/21/2919873.html

In addition, this configuration node file does not have to be placed in Web. config or app. config, for example, you can define a hibernate.cfg.xml file and then specify the location when you create Isessionfactory:

"hibernate.cfg.xml"). Buildsessionfactory (); 

Be careful here, "new Configuration (). Configure () ", If no Configure (" Xxx.cfg.xml ") is loaded from the configuration file, there is a custom file (such as: Xxx.cfg.xml) loaded.

Note that this hibernate.cfg.xml file name can be arbitrary, but set to always copy. Otherwise, the following error is reported:

  An exception occurred during configuration of persistence layer.

2, NHibernate mapping file must be set as an embedded resource.

  

3, NHibernate entity class all the attributes to be added virtual-virtual property, specifically in the persistence class of the article is explained, the following gives an example:

    Class Personmodel    {        intgetsetstringgetset;}}     

NHibernate as an ORM framework, it needs to know how to associate the table of the database with the corresponding entity class, and nhibernate to obtain this information by mapping the file.

Second, get the mapping file related configuration

The mapping file in NHibernate ends with a hbm.xml suffix, and there are 3 ways to get the mapping file NHibernate.

  1. Get the mapping file location from config program configuration file

NHibernate running on a. NET program, you need to provide yourself with a custom configuration node, which contains the database connection, what database and so on. This includes information about where the configuration file is located.

NHibernate reads the node directly from the configuration node on the Web. config or app. config file, and then finds the mapping file from the assembly specified by the node.

<mapping assembly = "Nx.repository"/>

The above configuration node holds the assembly where the mapping file is located by default. So when the program starts, NHibernate will automatically go to Nx.repository to find the *.hbm.xml mapping file.

If NHibernate cannot find the mapping file, the following error may be reported:

Server Error in "/" application. No persister for:xxx.xxx

2, the way of programming, written in the code

  (1), addfile specify the mapping file

Isessionfactory sessionfactory = new Configuration (). Configure (). AddFile (@ "C:\Users\Administrator\Desktop\NHibernate learning \model\mapping\person.hbm.xml"). Buildsessionfactory ();

Note that when you do not write the full path, that is, write only a file name, then NHibernate will read the assembly from the configuration file to the assembly specified in the configuration file to load the mapping file. If the configuration file does not specify an assembly, the mapping file for the specified name is looked up from the current assembly. But this way, you don't have to set the embedded resource, but remember to set it to always copy.

  Configuration cfg = new configuration (). AddFile ("Mapping/person.hbm.xml");  Isessionfactory sessionfactory = cfg. Buildsessionfactory ();

This method only specifies a file, but can be chained to specify multiple such as: AddFile ("F1"). AddFile ("F2").

  (2), addassembly

There is another way to specify an assembly when creating a configuration.

  Configuration cfg = new configuration (). addassembly ("Model");       Model is the name of the assembly where the mapping file is located  isessionfactory sessionfactory = cfg. Buildsessionfactory ();

  This way, even if the <mapping assembly= ""/> node is empty in the. config configuration file, NHibernate automatically finds all files that end with. Hbm.xml in the specified assembly.

  (3), addclass

This way, you can eliminate the program's hard-coded file path strings.

  Configuration cfg = new configuration (). AddClass (typeof (Model.person));  Isessionfactory sessionfactory = cfg. Buildsessionfactory ();

But the disadvantage is also obvious, the class name must be the same as the name of the mapping file. Otherwise, the compilation cannot be passed because the person class cannot be found. In addition, the mapping file Person.hbm.xml must be placed in the root directory.

NHibernate will automatically go to the program to find the file named Person.hbm.xml.

NHibernate Basic configuration (first 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.