- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
<map name="Images" lazy="true" table="ITEM_IMAGE" sort="natural"> in C#.NET
<map name="Images" lazy="true" table="ITEM_IMAGE" sort="natural"> Code 39 Full ASCII Generator In C#.NET Using Barcode generation for VS .NET Control to generate, create Code 3 of 9 image in Visual Studio .NET applications. www.OnBarcode.comCode-39 Decoder In C#.NET Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comMapping collections of value types
Code 128A Generation In Visual C#.NET Using Barcode generator for VS .NET Control to generate, create Code 128 image in VS .NET applications. www.OnBarcode.comEncode Data Matrix ECC200 In C#.NET Using Barcode maker for Visual Studio .NET Control to generate, create DataMatrix image in .NET applications. www.OnBarcode.com<key column="ITEM_ID"/> <index column="IMAGE_NAME" type="string"/> <element type="String" column="FILENAME" not-null="true"/> </map> 2D Encoder In C# Using Barcode creation for .NET framework Control to generate, create Matrix 2D Barcode image in Visual Studio .NET applications. www.OnBarcode.comUPC - 13 Generation In C#.NET Using Barcode creation for Visual Studio .NET Control to generate, create EAN-13 image in Visual Studio .NET applications. www.OnBarcode.comBy specifying sort="natural", you tell NHibernate to use a SortedMap, sorting the image names according to the CompareTo() method of System.String. If you want some other sorted order for example, reverse alphabetical order you can specify the name of a class that implements System.Collections.IComparer in the sort attribute. Here s an example: Generate PDF 417 In C#.NET Using Barcode encoder for VS .NET Control to generate, create PDF 417 image in .NET framework applications. www.OnBarcode.com2 Of 5 Interleaved Creation In C# Using Barcode drawer for VS .NET Control to generate, create Uniform Symbology Specification ITF image in Visual Studio .NET applications. www.OnBarcode.com<map name="Images" lazy="true" table="ITEM_IMAGE" sort="NHibernate.Auction.ReverseStringComparer, NHibernate.Auction"> <key column="ITEM_ID"/> <index column="IMAGE_NAME" type="string"/> <element type="String" column="FILENAME" not-null="true"/> </map> Code 39 Maker In None Using Barcode encoder for Online Control to generate, create Code 3/9 image in Online applications. www.OnBarcode.comScanning Code39 In None Using Barcode reader for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comNHibernate sorted maps use System.Collections.SortedList in their implementa- USS Code 128 Reader In None Using Barcode decoder for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comDataMatrix Reader In C# Using Barcode decoder for VS .NET Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comtion. A sorted set, which behaves like Iesi.Collections.SortedSet, is mapped in a similar way: 2D Creation In Java Using Barcode generator for Java Control to generate, create Matrix image in Java applications. www.OnBarcode.comBarcode Recognizer In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.com<set name="Images" lazy="true" table="ITEM_IMAGE" sort="natural"> <key column="ITEM_ID"/> <element type="String" column="FILENAME" not-null="true"/> </set> QR Code Drawer In Visual Basic .NET Using Barcode creation for .NET framework Control to generate, create QR-Code image in Visual Studio .NET applications. www.OnBarcode.comBarcode Drawer In None Using Barcode generation for Office Excel Control to generate, create Barcode image in Microsoft Excel applications. www.OnBarcode.comBags can t be sorted, and there is no SortedBag, unfortunately. Nor may lists be sorted, because the order of list elements is defined by the list index. Alternatively, you may choose to use an ordered map, using the sorting capabilities of the database instead of in-memory sorting: Drawing Code 128 Code Set B In None Using Barcode drawer for Font Control to generate, create Code 128C image in Font applications. www.OnBarcode.comBarcode Drawer In Java Using Barcode maker for Eclipse BIRT Control to generate, create Barcode image in BIRT reports applications. www.OnBarcode.com<map name="Images" lazy="true" table="ITEM_IMAGE" order-by="IMAGE_NAME asc"> <key column="ITEM_ID"/> <index column="IMAGE_NAME" type="String"/> <element type="String" column="FILENAME" not-null="true"/> </map> QR Code JIS X 0510 Encoder In Java Using Barcode generator for BIRT Control to generate, create QR Code ISO/IEC18004 image in BIRT reports applications. www.OnBarcode.comMake Barcode In .NET Framework Using Barcode printer for .NET Control to generate, create Barcode image in VS .NET applications. www.OnBarcode.comThe expression in the order-by attribute is a fragment of a SQL order by clause. In this case, you order by the IMAGE_NAME column, in ascending order. You can even write SQL function calls in the order-by attribute: <map name="Images" lazy="true" table="ITEM_IMAGE" order-by="lower(FILENAME) asc">
Advanced mapping concepts
<key column="ITEM_ID"/> <index column="IMAGE_NAME" type="String"/> <element type="String" column="FILENAME" not-null="true"/> </map> Notice that you can order by any column of the collection table. Both sets and bags accept the order-by attribute; but again, lists don t. This example uses a bag: <idbag name="Images" lazy="true" table="ITEM_IMAGE" order-by="ITEM_IMAGE_ID desc"> <collection-id type="Int32" column="ITEM_IMAGE_ID"> <generator class="sequence"/> </collection-id> <key column="ITEM_ID"/> <element type="String" column="FILENAME" not-null="true"/> </idbag> Under the covers, NHibernate uses an Iesi.Collections.ListSet and a System. Collections.Specialized.ListDictionary to implement ordered sets and maps; use this functionality carefully because it doesn t perform well with large numbers of elements. In a real system, it s likely that you ll need to keep more than just the image name and filename; you l probably need to create an Image class to store this extra information. Of course, you could map Image as an entity class; but because we ve already concluded that this isn t absolutely necessary, let s see how much further you can get without an Image entity, which would require an association mapping and more complex lifecycle handling. In chapter 3, you saw that NHibernate lets you map user-defined classes as components, which are considered to be value types. This is still true, even when component instances are collection elements. Let s now look at how you can map collections of components. Collections of components
The Image class defines the properties Name, Filename, SizeX, and SizeY. It has a single association, with its parent Item class, as shown in figure 6.7. As you can see from the aggregation association style depicted by a black diamond, Image is a component of Item, and Item is Name : string the entity that is responsible for the lifecycle Description : string of Image. References to images aren t InitialPrice : double Name : string ReservePrice : double Filename : string shared, so our first choice is an NHibernate StartDate : DateTime SizeX : int EndDate : DateTime SizeY : int component mapping. The multiplicity of the Created : DateTime association further declares this association as many-valued that is, zero or more Images Figure 6.7 Collection of for the same Item. Image components in Item Mapping collections of value types
WRITING THE COMPONENT CLASS
First, you implement the Image class. This is a POCO, with nothing special to consider. As you know from chapter 4, component classes don t have an identifier property. But you must implement Equals() and GetHashCode() to compare the Name, Filename, SizeX, and SizeY properties. This allows NHibernate s dirty checking to function correctly. Strictly speaking, implementing Equals() and GetHashCode() isn t required for all component classes; but we recommend it for any component class because the implementation is fairly easy, and better safe than sorry is a good motto. The Item class hasn t changed, but the objects in the collection are now Images instead of Strings. Let s map this to the database.
|
|