NHibernate development, must pass phase: Need entity and XML mapping, this section focuses on this piece of content.
For novice developers, how to map the database and XML mapping, is not cut, the following start introduction:
One: What is nhibernate mapping?
In fact, it is in the database and entities, put more than one external storage media XML, to link the relationship between the two, when the database changes, by modifying the XML configuration, to achieve mapping of the switch, so no need to modify the code.
Let's look at a simple XML that maps between databases and entities:
<?xml version= "1.0" encoding= "Utf-8"?>
<class name= "class name" table= "table name" lazy= "true" >
<id name= "id" column= "id" type= "int" >
<generator class= "Assigned"/>
</id>
<property name= "Name" type= "string" >
<column name= "name" length= "Wuyi"/>
</property>
</class>
It is also very simple, basically because there are too many tables or fields, this kind of mapping usually needs to use the NHibernate tool to build.
Two: nhibernate mapping label
is actually an explanation of the various meanings of the mapping XML above:
1: We first look at the root node, of course, the following content than the above: hibernate-mapping tags
Schema= "schemaName" Database schema name
Default-cascade= "None|save-update" cascading style (default none)
auto-import= whether the "True|false" query statement can use a non-fully qualified name
DLL name of class for assembly= "AssemblyName" Mapping file
namespace name of the class for the Namespace= "namespace" mapping file
/>
2:nhibernate class Label
<class
Name= the full name of the "ClassName" class.
Table= the "tablename" database table name.
Discriminator-value= "Discriminator_value" distinguishes the distinguished values of different subclass values, using polymorphic behavior.
Mutable= whether an instance of the "True|false" class is variable.
Schema= "owner" overrides the schema name specified in the root element.
Proxy= the name of the "Proxyinterface" proxy class.
Dynamic-update= "True|false" dynamically generates an UPDATE statement.
Dynamic-insert= "True|false" dynamically generates INSERT statements.
Select the Select-before-update= "True|false" before updating
polymorphism= "implicit|explicit" implicit query or explicit query.
where= "arbitrary SQL Where condition" attaches the Where condition, which is automatically added in the statement.
Persister= "Persisterclass" Custom Iclasspersister.
Batch-size= "N" batch read quantity [Default 1].
optimistic-lock= the "None|version|dirty|all" locking policy.
Whether the lazy= "True|false" is delayed loading.
Abstract= "True|false" is an abstract class.
/>
3:nhibernate ID Label
<id
Name= the "PropertyName" Entity property name.
Type= "TypeName" NHibernate type name
column= the "column_name" database field name.
Unsaved-value= the "Any|none|null|id_value" instance is not saved.
Access= "field|property|nosetter| ClassName "NHibernate the policy used to access property values.
<generator class= "Generatorclass"/>
</id>
4:nhibernate Property Label
<property
Name= "PropertyName"Entity attribute name
Column= "column_name"database field Name
。
Type= "TypeName"NHibernate type name
。
Update= "True|false" contains this field if it is update.
Insert= "True|false" contains the field if it is insert.
Formula= "arbitrary SQL expression" SQL expression that defines the value of this computed property
Access= "field|property| ClassName "NHibernate policy for accessing property values
optimistic-lock= "True|false" optimistic locking strategy
generated= whether the "Never|insert|always" property value is generated by the database
/>
Three: NHibernate mapping type
1:nhibernate
Value type Mappings
nhibernate Type |
. NET Type |
Database Type |
Notes |
Ansichar |
System.Char |
Dbtype.ansistringfixedlength-1 Char |
|
Boolean |
System.Boolean |
Dbtype.boolean |
The default value when no type attribute is specified. |
Byte |
System.Byte |
Dbtype.byte |
The default value when no type attribute is specified. |
Char |
System.Char |
Dbtype.stringfixedlength-1 Char |
The default value when no type attribute is specified. |
Datetime |
System.DateTime |
Dbtype.datetime-ignores the milliseconds |
The default value when no type attribute is specified. |
Decimal |
System.Decimal |
Dbtype.decimal |
The default value when no type attribute is specified. |
Double |
System.Double |
Dbtype.double |
The default value when no type attribute is specified. |
Guid |
System.Guid |
Dbtype.guid |
The default value when no type attribute is specified. |
Int16 |
System.Int16 |
Dbtype.int16 |
The default value when no type attribute is specified. |
Int32 |
System.Int32 |
Dbtype.int32 |
The default value when no type attribute is specified. |
Int64 |
System.Int64 |
Dbtype.int64 |
The default value when no type attribute is specified. |
Persistentenum |
System.Enum |
Potential type corresponding to DbType |
You do not have to specify type= "Persistentenum" in the mapping file. Instead, the full name of the enumerated assembly is provided, allowing the nhibernate to guess the type with reflection. The potential types used by enumerations determine the appropriate dbtype. |
Single |
System.Single |
Dbtype.single |
The default value when no type attribute is specified. |
Ticks |
System.DateTime |
Dbtype.int64 |
Type= "Ticks" must be specified. |
TimeSpan |
System.TimeSpan |
Dbtype.int64 |
The default value when no type attribute is specified. |
Timestamp |
System.DateTime |
Dbtype.datetime-Depends on database support |
Type= "Timestamp" must be specified. |
TrueFalse |
System.Boolean |
Dbtype.ansistringfixedlength-a character, ' Y ' or ' N ' |
Type= "TrueFalse" must be specified. |
Yesno |
System.Boolean |
Dbtype.ansistringfixedlength-a character, ' Y ' or ' N ' |
Type= "Yesno" must be specified. |
2: NHibernate Apply Type mappings
NHibernate Type |
. NET Type |
Database Type |
remarks |
Ansistring |
System.String |
Dbtype.ansistring |
Type= "Ansistring" must be specified. |
CultureInfo |
System.Globalization.CultureInfo |
Dbtype.string-5 characters indicating culture (culture) |
The default value when no type attribute is specified. |
Binary |
System.byte[] |
Dbtype.binary |
The default value when no type attribute is specified. |
Type |
System.Type |
Dbtype.string Save the application's centralized qualified name. |
The default value when no type attribute is specified. |
String |
System.String |
Dbtype.string |
The default value when no type attribute is specified. |
3: NHibernate binary Type mappings
NHibernate Type |
. NET Type |
Database Type |
remarks |
Stringclob |
System.String |
Dbtype.string |
Type= "Stringclob" must be specified. The entire field is readable in memory. |
Binaryblob |
System.byte[] |
Dbtype.binary |
Type= "Binaryblob" must be specified. The entire field is readable in memory. |
Serializable |
Any System.Object must annotate a serializable label |
Dbtype.binary |
Type= "Serializable" should be specified. If you cannot find the NHibernate type for a property, this is the last type to rely on. |
NHibernate Summary:
This section mainly introduces the knowledge of entity and XML mapping in NHibernate development.
The
is also a verifiable document, in the development process can be a lot of reference to help you develop.