Set, list, map, bag, array in the nhib set ing

Source: Internet
Author: User

Semantics of set, bag, list, and map
The most important thing about a set is the semantics of the set. The Set, List, Collection, and Map collections are defined in Java JPA (Java collections allow the bag semantics). NHibernate copies these concepts during Hibernate transplantation. However, in the. Net community, these concepts are relatively weak. Many people are unfamiliar with the set, bag, and map statements, which also causes certain obstacles to the use of NHibernate set ing.
Set
Objects in a set are unique and unordered. They cannot be accessed through indexes or key values. You can only use enumerator to list collection objects.
. Net does not have a native set class, so nhibctions uses the set of Iesi. Collections.
Different set implementations may have some differences. As a result, the set features are not the same, but the main principle in design and use is to regard the set as unique and unordered.
Iesi. in Collections. collections. the ListSet implemented by SortedList is in the same order as that added to the set when listing the set objects, but is based on System. collections. the HashSet implemented by HashTable is inconsistent. HashSet determines whether the object is equal based on the GetHashCode () returned value of the object, while ListSet uses the Equals () method of the object to determine whether the object is equal. Therefore, if you do not need to overload the GetHashCode () and Equals () methods, there is a problem in ensuring uniqueness. HybridSet is Iesi. A mixed type in Collections, based on System. collections. specialized. hybridDictionary is implemented mainly for the sake of performance. The internal implementation will automatically convert between the ListSet and HashSet types based on the number of objects in the set.
In addition, SortedSet in Iesi. Collections allows an IComparer interface to sort collection objects by the method provided by IComparer.
Bag:
Similar to set, the only difference is that repeated objects are allowed in bag.
. Net does not have a native bag class, And PowerCollections has a bag implementation.
List:
An ordered set that can be repeated. An integer starting from 0 is used as an index.
In. Net, the List, ArrayList, and ArrayList are implemented using arrays, and the List is implemented using Bidirectional linked lists.
Map:
Unordered set. key values cannot be repeated. You can use any type of objects as indexes.
In. Net, map classes include Dictionary, SortedDictionary, HashTable, and SortedList. SortedDictionary provides sorting support.
In Java, map has HashMap and SortedMap.

Correspondence between set, bag, list, and map semantics and System. Collections

Under System. Collections, the IList and list semantics are consistent, and the IDictionary and map semantics are consistent. For specific implementation classes, determine the correspondence with the set, bag, list, and map semantics based on their Implemented interfaces.
For example, SortedList mentioned above has an IDictionary interface, so it is a map implementation, which makes its name and semantics inconsistent.
The ICollection interface does not explicitly declare the semantics. It is entirely determined by the implementation of a specific class. Abstract class CollectionBase implements the IList and ICollection interfaces, so it is the list semantics. The Bag <T> In PowerCollections is implemented based on ICollection.
Difference between Entity and Value Object in set ing
An object has an independent life cycle and its own Entity Identifier. the life cycle of a value object is completely dependent on the object to which it belongs, and it does not have its own Identifier ).
The most basic value types are. Net native value types. Common simple value objects in the business, such as Money, European and American style names, etc. Complex business entities and value objects are sometimes difficult to distinguish, and the same concept object may be different in different system environments, for example, in a professional Geographic Information Processing System, addresses may be entities, while addresses may be value objects for other systems such as ERP and human resource management.
Some simple principles can be used to distinguish between objects and value objects:
1. lifecycle. A value object cannot exist without the entity it belongs to. It is created at or after the entity creation and destroyed together with the entity it belongs. This life cycle is for the business side. If you understand the life cycle of objects in the memory, there will be doubts. Therefore, it is combined with the records in the database (that is, objects in the persistent State) to understand the concept of lifecycle more intuitive.
2. Shared references. Value objects cannot be shared. Take the address as an example. If both user A and user B have the same address, there will be two records in the address table when designed as A value object. One belongs to user A and the other belongs to user B, when designed as an object, there is only one record in the address table, and both user A and user B are referenced by the ID of the address object. Which scheme should be adopted depends on the specific business needs.
Rough design treats value objects and entities equally, and a slightly more detailed design will show many value objects.
In NHibernate, <set>, <bag>, <list>, and <map> are combined with <element> and <composite-element> to map the set of value objects; the appearance of one-to-one, sequence-to-one, one-to-sequence, and sequence-to-sequence indicates the association between entities.
Cascade is not required for set ing of value objects, because its lifecycle principle has been determined in definition. cascade is only used for inter-entity lifecycle Association Control.
In set ing, the value object and the object are stored in different tables, but the value object does not need an independent configuration file. In the object ing file, use <set>, <bag>, <list> and <map> are completely described. Each object has its own configuration file.
<Set>, <bag>, <list>, and <map>
Select an appropriate ing configuration node from <set>, <bag>, <list>, and <map> according to the semantics. The following table shows the relationship between the configuration node and the interface type:

Mapping NodeInterface

<Set> Iesi. Collections. ISet

<Bag> IList

<List> IList

<Map> IDictionary

The corresponding set property must be declared using the interface, because the set object type returned by nhib.pdf is its internal implementation of these interfaces, namely, nhib.pdf. persistentSet, PersistentList, PersistentBag, and PersistentMap in the Collection namespace, rather than ListSet and HashTable. The specific classes that use these interfaces according to NHibernate, which can be automatically converted during access.
. Net does not implement set and bag, so nhibctions uses the set of Iesi. Collections and IList to simulate the bag semantics.
If you do not want to use these interfaces for properties, you can use access. Set the field to the interface type. In the get and set methods of the property, type conversion is completed. Through the access settings in the property configuration, NHibernate bypasses get and the set method directly accesses the field.

<Set name = "Addresses" table = "USER_ADDRESS">
<Key column = "USER_ID"/>
<Element column = "ADDRESS" type = "String"/>
</Set>
<Set name = "Addresses" table = "USER_ADDRESS">
<Key column = "USER_ID"/>
<Composite-element class = "Address">
<Property name = "Country" column = "COUNTRY" type = "String"/>
<Property name = "Province" column = "PROVINCE" type = "String"/>
<Property name = "ZipCode" column = "ZIP_CODE" type = "String"/>
<Property name = "AddressText" column = "ADDRESS_TEXT" type = "String"/>
</Composite-element>
</Set>

The above is the set ing of value objects. If it is a set ing between entities, you can use <sequence-to-sequence> and <one-to-sequence> in <set>.
The usage of <bag> is exactly the same as that of <set>. They are only semantically different.
<List> because it is an ordered set, an integer index field is required to ensure the order of data in physical storage. The value of this field is automatically maintained by nhib.pdf. Others are exactly the same as <set>.

<List name = "Addresses" table = "USER_ADDRESS">
<Key column = "USER_ID"/>
<Index column = "ADDR_INDEX"/>
<Element column = "ADDRESS" type = "String"/>
</List>

The difference between <map> and <list> is that the index of <list> is an integer, while the index of <map> can be of any type, therefore, the <index> node in <map> can specify the type attribute. In some cases, other entities may be used as the index through association, so NHibernate provides another index configuration method for <map>, <index-relative-to-sequence> and <index-relative-to-any>. Other configurations are the same as those of <set>.
Sequence of set Elements
<Set>, <bag>, <map> data is stored unordered. NHibernate supports sorting when obtaining data. There are two sorting methods: sort and order-by. For detailed usage, see nhib? 03 many-to-sort set httping http://www.cnblogs.com/riccc/archive/2007/04/08/704557.html.
The sorting during loading differs from the sequential storage of the list. The list can retain the order added to the set, and the sorting during loading is fixed, therefore, it is not a completely alternative solution.
<Idbag>
<Relative-to-sequence>: A set ing between values and objects. The associated table and the value object table do not have their own identification fields. <idbag> is used to identify fields.

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.