Chapter 1 collection class ing
Directory
-
Persistent Collections)
-
Ing a Collection)
-
Collections of Values and pairs-To-pair Associations)
-
One-To-multiple Associations)
-
Lazy Initialization)
-
Sorted Collections)
-
Use <idbag>
-
Bidirectional Associations)
-
Ternary Associations)
-
Heterogeneous Associations)
-
Set example
Persistent Collections)
This part does not contain a large number of. NET code examples. We assume that you have learned how to use. NET's collection framework and Set. In fact, in this case, there is really nothing to learn... to make a summary in one sentence, you will use the knowledge you have mastered to use them.
Nhib.pdf can persist instances of the following sets, includingSystem.Collections.IDictionary
,System.Collections.IList
,Iesi.Collections.ISet
And any persistent object or value array. Type:System.Collections.ILst
You can also use the "bag" syntax for persistence.
Warning: For a persistent set, except for the set interface, you cannot retain the semantics attached to any classes that implement these interfaces (for example:Iesi.Collections.ListSet
Iteration order ). In fact, all persistence sets followSystem.Collections.Hashtable
,System.Collections.ArrayList
,Iesi.Collections.HashedSet
. More deeply, for an attribute containing a set, the. NET type must be defined as an interface (that isIDictionary
,IList
OrISet
). The reason for this restriction is that when you don't know it, NHibernate Secretly Put yourIDictionary
,IList
AndISet
The instance is replaced with its own implementation of these sets. (So be careful when using the = Operator in your program .)
Cat cat = new DomesticCat();Cat kitten = new DomesticCat();...Iesi.Collections.ISet kittens = new Iesi.Collections.HashedSet(); kittens.Add( kitten );cat.Kittens = kittens;session.Save( cat );kittens = cat.Kittens; // Okay, kittens collection is an ISet(Iesi.Collections.HashedSet)cat.Kittens; //Error! - a NHibernate.Collections.Set not Iesi.Collections.HashedSet
A set follows a common rule for the value type: A reference cannot be shared and Its contained entities coexist. Because of the underlying association model, the set does not support null semantics, and NHibernate does not distinguish a null set reference from an empty set with no elements.
When a collection class is referenced by a persistent object, it is automatically persisted and deleted when it is no longer referenced. If a set is passed from one persistent object to another, its elements may be transferred from one table to another. You do not need to care about this. Just like using a normal. NET collection class, you need to be sure that you understand the semantics of Bidirectional association before using it (which will be discussed later ).
The Set instance is differentiated in the database based on the foreign key pointing to the corresponding object. This foreign key is called a set keyword. Use<key>
Element to map the keywords of this set.
A set can contain almost all other NHibernate types, including all basic types, custom types, object types, and components. There is an important definition: objects in a set can be operated through the "pass value" Semantics (completely dependent on the set itself), or a reference pointing to other entities, has its own lifecycle. The set cannot contain other sets. The types of these contained elements are called set element types. Elements of the set are mapped<element>
,<composite-element>
,<one-to-many>
,<many-to-many>
Or<many-to-any>
. The first two types Use Value-passing semantic operation elements, and the other three are mapped to object associations.
BesidesISet
And all Collection types outside of Bag have an index field, which is mapped to an array orIList
OrIDictionary
.IDictionary
The index type can be any basic type, entity type, or even a combination type (but not a collection type ). Array andIList
The index must be an integer (Int32
). Use<index>
,<index-many-to-many>
,<composite-index>
Or<index-many-to-any>
And other elements to map the index.
Collection classes can generate many types of mappings, covering many common relational models. We recommend that you practice using the schema generation tool to understand how to convert different ing definitions into database tables.
> One-To-Many Associations (One-To-Many Associations) <Lazy Initialization (Sorted Collections ctions) use <idbag> Bidirectional Associations)
Two-way Association allows access to the other end through either end of the association. In NHibernate, two types of Bidirectional Association are supported:
-
One-to-multiple)
-
<set>
Or<bag>
The value is at one end, and the independent value (non-set) is at the other end.
-
Many-to-many)
-
Both ends are<set>
Or<bag>
Value
Note that nhibtionary does not support a set with indexes (IList, IDictionary, or array) as the two-way one-to-one association of the end of "multiple". You must use a set or bag ing.
To establish a two-way multi-to-many Association, you only need to map Two Mappings-to-pair to the same database table, define one end as inverse (which end should be based on your choice ). Here is an example of Bidirectional Association from a class to its own category-to-category (each category can have many items, and each items can belong to many categories ):
<class name name="NHibernate.Auction.Category, NHibernate.Auction"> <id name="Id" column="ID"/> ... <bag name="Items" table="CATEGORY_ITEM" lazy="true"> <key column="CATEGORY_ID" /> <many-to-many class="NHibernate.Auction.Category, NHibernate.Auction" column="ITEM_ID" /> </bag></class><class name="NHibernate.Auction.Item, NHibernate.Auction"> <id name="Id" column="ID" /> <!-- inverse end --> <bag name="Categories" table="CATEGORY_ITEM" inverse="true" lazy="true"> <key column="ITEM_ID" /> <many-to-many class="NHibernate.Auction.Category, NHibernate.Auction" column="CATEGORY_ID" /> </bag></class>
If only the reverse end of the association is changed, the change will not be persisted. This indicates that each two-way Association has two performances in the memory, one connecting from A to B, and the other connecting from B to. If you think about the. NET object model, how do we create many-to-many relationships in. NET makes it easier to understand:
category.Items.Add( item ); // The category now "knows" about the relationshipitem.Categories.Add( category ); // The item now "knows" about the relationshipsession.Update( item ); // No effect, nothing will be saved!session.Update( category ); // The relationship will be saved
Non-reverse ends are used to save the memory representation to the database. If both ends are adapted, we will perform additional INSERT/UPDATE operations, or even get a foreign key conflict! This is also true for Bidirectional one-to-multiple associations.
To establish a one-to-multiple bidirectional Association, you can use a one-to-many association as a multi-to-one ing to the fields of the same table, and defined at the end of "multiple"inverse="true"
.
<class name="Eg.Parent, Eg> <id name="Id" column="id" /> ... <set name="Children" inverse="true" lazy="true"> <key column="parent_id" /> <one-to-many class="Eg.Child, Eg" /> </set></class><class name="Eg.Child, Eg"> <id name="Id" column="id" /> .... <many-to-one name="Parent" class="Eg.Parent, Eg" column="parent_id" /></class>
Defined at the end of "1"inverse="true"
Cascade operations are not affected. They are different concepts!
An example of a set of Ternary Associations