1. The base map commonly used tags
NHibernate as the ORM framework for:
Class is an entity corresponding to a table in the database;
A corresponding one class attribute field in a table;
An object corresponds to a record in the table.
1.1 hibernate-mapping
<Hibernate-mapping> tag is the root node NHibernate mapping file.
<Mapping Hibernate-
schema = "schemaName" database schema name.
default-cascade = "none | save-update" default cascade style (optional - defaults to none) :.
auto-import = "true | false" Specifies whether to use that we can use in a query when a non-fully qualified name.
assembly="AssemblyName"
namespace = Application class "Namespace" to specify the mapping file and set it in the name of the namespace name,
Used to generate non-fully qualified name of the class.
/>
1.2 class
< Class > tag defines a persistent class
<class
name = "ClassName" persistent class class name, here is the full name of the class.
table = "tableName" corresponding database table.
discriminator-value = "discriminator_value" discrimination value, a value that distinguishes the different subclasses for using (inherited course mapped will be mentioned later) in polymorphic behavior.
mutable = "true | false" indicates that the variable instances of the class.
schema = "owner" coverage specified in the root element schema name.
proxy = "ProxyInterface" specifies an interface, when used as a proxy loading delay. You can use this type their names here.
dynamic-update = "true | false " Specifies UPDATE SQL should be generated at runtime and contain only those columns whose values have changed.
dynamic-insert = "SQL should be generated at runtime, and only those containing a non-null value field.
select-before-update = "true | false" Specifies the NHibernate unless it is determined the object is actually modified, UPDATE operation.
polymorphism = "implicit | explicit" Determines whether implicit or explicit query polymorphism.
where = "arbitrary sql where condition" specify an arbitrary SQL the WHERE condition, while crawling objects of this class will always increase this condition.
persister = "PersisterClass" specify a custom IClassPersister .
batch-size = "N" for a specified time using the example of the identifier fetch "batch size" (the number of batch fetch), the default value is 1.
optimistic-lock = "none | version | dirty | all" optimistic locking, decided optimistic locking strategy.
lazy = "true | false" lazy loading is enabled.
abstract = "true | false" whether it is an abstract class.
/>
1.3 id
<id> tag defines the attribute database table to the primary key field mappings.
<id
name = name "PropertyName" identifier property.
type = "typename" NHibernate type the name
column = name "column_name" primary key field.
unsaved-value = "any | none | null | id_value" a specific identifier property value that indicates an instance is newly created, not yet saved.
access = "field | property | nosetter | ClassName" NHibernate used to access the property value of the policy.
<Generator class = "generatorClass" />
</ ID>
generator: primary key generation strategy
NHibernate provides the following generation strategy:
2. The composite primary key
2.1 composite-id
<Composite-id> is the primary key.
<Composite-the above mentioned id
name = "PropertyName"
class = "ClassName" primary key class is the class name of the
criteria specified by unsaved-value = "the any | none"
Access = "Field, | Property | nosetter | ClassName">
<Key-Property name = "PropertyName" type = "typename" column = " column_name" /> primary key attribute of the
<key-many-to-one name = "PropertyName class =" ClassName "column =" column_name "/> many-to-primary key attributes
.... ..
</ Composite-ID>
Note that when using the primary key, your persistent class must override Equals () and GetHashCode () method
code show as below:
Product.hbm.xml
< hibernate-mapping xmlns ="urn:nhibernate-mapping-2.2" assembly ="NHibernate3.Domain" namespace ="NHibernate3.Domain" >
< class name ="Product" table ="T_Product2" lazy ="true" >
< composite-id name ="ID" class ="ProductID" >
< key-property name ="Name" type ="string" >
< column name ="Name" length ="50" />
</ key-property >
< key-property name ="QuantityPerUnit" type ="string" >
< column name ="QuantityPerUnit" length ="50" />
</ key-property >
</ composite-id >
< property name ="Unit" type ="string" >
< column name ="Unit" length ="50" />
</ property >
< property name ="SellPrice" type ="decimal" >
< column name ="SellPrice" precision ="14" scale ="2" />
</ property >
< property name ="BuyPrice" type ="decimal" >
< column name ="BuyPrice" precision ="14" scale ="2" />
</ property >
< property name ="Remark" type ="string" >
< column name ="Remark" length ="200" />
</ property >
</ class >
</ hibernate-mapping >
ProductID.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernate3.Domain
{
public class ProductID
{
public virtual string Name { get; set; }
public virtual string QuantityPerUnit { get; set; }
public override bool Equals( object obj)
{
var entity = obj as ProductID;
if (entity == null)
{
return false;
}
return entity.Name == this.Name
&& entity.QuantityPerUnit == this.QuantityPerUnit;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
}
}
Product.cs
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NHibernate3.Domain
{
public class Product
{
public virtual ProductID ID { get; set; }
public virtual string Unit { get; set; }
public virtual decimal SellPrice { get; set; }
public virtual decimal BuyPrice { get; set; }
public virtual string Remark { get; set; }
}
}
running result:
Reproduced in: https: //www.cnblogs.com/davidgu/archive/2012/06/15/2550728.html