NHibernate collection map in Depth (fifth) <set>,<list>,<map>,<bag> I. Set FOREIGN key
In NHibernate, the typical elements used to map the collection class are <set>,<list>,<map>,<bag>,<array>,<primitive-array >.
We go back to the previous one, the set of mappings based on a pair of multiple queries. for collection types:
iset<personmodel> get; set;}
The NHibernate configuration file corresponds to the following:
<SetName= "Listperson"Table= "Person"Generic= "true" Inverse= "true" > Span style= "color: #0000ff;" ><key column= " Countryid " Foreign-key=" Fk_person_country "/> <!-- person's foreign key column name --> <class= "Model.personmodel,model" /> </set>
So what are the uses of <set>, <list>, <map>, <bag>, <array>, <primitive-array>, and what is the difference between these things?
1.<set>
The objects in the Iset<model> collection are unique, unordered, and cannot be accessed through an index or key value.
There are no classes in. NET that correspond to <set>. So when our configuration file is a <set> config set relationship, the Model persistence class iset<model> must use the NHibernate iesi.collections.
2.<bag>
<bag> like <set> Basically, the only difference is that the duplicate objects are allowed in the bag.
The collection type that corresponds to the <bag> configuration is ilist<model>.
3.<list>
An ordered set, which can be repeated, using an integer starting from 0 as the index. Therefore, you must have <INDEX> when configuring the <list> node, and the column type specified by index must be integer.
The collection type that corresponds to the <list> configuration is ilist<model>.
4.<map>
Unordered collections, key values cannot be duplicated, values can be duplicated, and objects of any type can be used as indexes.
The collection type that corresponds to the <map> configuration is idictionary<key,value>.
First we select the appropriate mapping configuration node from <set>, <bag>, <list>, based on the relationship between the data. The following table is the relationship between the configuration node and the interface type:
The corresponding collection property must use an interface declaration, because the collection object type returned by NHibernate is an implementation of these interfaces within it, that is, the Persistentset, Persistentlist, and Nhibernate.collection under the namespace. Persistentbag, Persistentmap, and not Listset, Hashtable and so on. The specific classes of these interfaces are used in accordance with NHibernate, and NHibernate can be automatically converted when accessed.
. NET does not have set and bag implementations, so NHibernate uses the iesi.collections set to simulate bag semantics using IList.
If the properties do not want to use these interfaces, you can use Access. Set field to the interface type, complete the type conversion in the property's get, set method, pass the access setting in the properties configuration, let NHibernate bypass get, set method directly access field.
<ListName= "Addresses"Table= "user_address" > key column= "User_ ID "/> <index Span style= "color: #ff0000;" >column= "Addr_index" /> <element column< Span style= "color: #0000ff;" >= "ADDRESS" Type=" String " /></list>
The difference between <map> and <list> is that the index of,<list> is integral, and the index of <map> can be any type, so <map> <index> A node can specify the Type property. In some cases, it is possible to use other entities as index by association, so NHibernate provides an alternative index configuration for <map>,<index-many-to-many> and <index-many- To-any>. Other configurations are the same as <set>.
|
Set |
Bag |
List |
Map |
Whether to allow duplicates |
Whether |
Is |
Is |
Key cannot be duplicated, value can be repeated |
Explicit sorting |
Whether |
Whether |
Is |
Whether |
For a type |
Iesi.Collections.ISet |
IList |
IList |
IDictionary |
Four of the difference is on the above, according to the actual situation, to choose.
NHibernate collection mapping in Depth (fifth) <set>,<list>,<map>,<bag>