`
wangtong40
  • 浏览: 248735 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Hibernate的集合映射

    博客分类:
  • ORM
阅读更多

<!-- [if !mso]> <style> v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);} </style> <![endif]--><!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <style> /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} </style> <![endif]-->

<!-- [if !supportLists]-->节 1.01   <!-- [endif]-->映射 Set

<!-- [if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"> <v:stroke joinstyle="miter"/> <v:formulas> <v:f eqn="if lineDrawn pixelLineWidth 0"/> <v:f eqn="sum @0 1 0"/> <v:f eqn="sum 0 0 @1"/> <v:f eqn="prod @2 1 2"/> <v:f eqn="prod @3 21600 pixelWidth"/> <v:f eqn="prod @3 21600 pixelHeight"/> <v:f eqn="sum @0 0 1"/> <v:f eqn="prod @6 1 2"/> <v:f eqn="prod @7 21600 pixelWidth"/> <v:f eqn="sum @8 21600 0"/> <v:f eqn="prod @7 21600 pixelHeight"/> <v:f eqn="sum @10 21600 0"/> </v:formulas> <v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/> <o:lock v:ext="edit" aspectratio="t"/> </v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style="width:197.25pt; height:192pt" mce_style="width:197.25pt; height:192pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

<!-- [if !supportLists]-->1、  <!-- [endif]-->为了隐藏 ITEM_IMAGE 为一个单独表,故在数据库设计中将 ITEM_IMAGE 的主键和外键设定都设定为 ITEM_ID, Hibernate 配置文件中用 set 集合的形式加以隐藏。

<!-- [if !supportLists]-->2、  <!-- [endif]-->引文 Set 集合无法包含重复元素,因此在 ITEM_IMAGE 表中使用复合主键。

 

不可重复的元素,顺序无法保存

private Set images = new HashSet ();

< set name = "images" table = "ITEM_IMAGE" >

    < key column = "ITEM_ID" />

    < element type = "string" column = "FILENAME" not-null = "true" />

</ set >

<!-- [if !supportLists]-->节 1.02   <!-- [endif]-->映射 Bag

可重复的元素,不保存元素顺序

<!-- [if gte vml 1]><v:shape id="_x0000_i1026" type="#_x0000_t75" style="width:197.25pt;height:192pt" mce_style="width:197.25pt;height:192pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

 

private Collection images = new ArrayList ();

       < idbag name = "images" table = "ITEM_IMAGE" >

           < collection-id type = "long" column = "ITEM_IMAGE_ID" >

              < generator class = "sequence" />

           </ collection-id >

           < key column = "ITEM_ID" />

           < element type = "string" column = "FILENAME" not-null = "true" />

       </ idbag >

<!-- [if !supportLists]-->节 1.03   <!-- [endif]-->映射 List

可重复的元素,顺序可以保存

<!-- [if gte vml 1]><v:shape id="_x0000_i1027" type="#_x0000_t75" style="width:208.5pt;height:199.5pt" mce_style="width:208.5pt;height:199.5pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image003.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image003.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

<!-- [if !supportLists]-->1、  <!-- [endif]-->添加 POSITION 列目的是用于保存存储数据的顺序。

<!-- [if !supportLists]-->2、  <!-- [endif]-->表的主键为 ITEM_ID POSITION 的复合主键。

private List images = new ArrayList();

       < list name = "images" table = "ITEM_IMAGE" >

           < key column = "ITEM_ID" />

           < list-index column = "POSITION" />

           < element type = "string" column = "FILENAME" not-null = "true" />

       </ list >

 

<!-- [if !supportLists]-->节 1.04   <!-- [endif]-->映射 Map

保存键 / 值对数据 , 顺序不可保存,为无序状态

<!-- [if gte vml 1]><v:shape id="_x0000_i1028" type="#_x0000_t75" style="width:235.5pt;height:224.25pt" mce_style="width:235.5pt;height:224.25pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image005.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image005.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

1 、将 ITEM_ID IMAGENAME 设定为双主键。

 

 

private Map images = new HashMap ();

       < map name = "images" table = "ITEM_IMAGE" >

           < key column = "ITEM_ID" />

           < map-key type = "string" column = "IMAGENAME" />

           < element type = "string" column = "FILENAME" not-null = "true" />

       </ map >

<!-- [if !supportLists]-->节 1.05   <!-- [endif]-->排序集合 Sorted Collection

<!-- [if !supportLists]-->(a)      <!-- [endif]-->排序 Set

<!-- [if gte vml 1]><v:shape id="_x0000_i1029" type="#_x0000_t75" style="width:197.25pt;height:192pt" mce_style="width:197.25pt;height:192pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

Set images = new TreeSet ();

< set name = "images" table = "ITEM_IMAGE" sort = "natural" >

           < key column = "ITEM_ID" />

           < element type = "string" column = "FILENAME" not-null = "true" />

</ set >

<!-- [if !supportLists]-->(b)      <!-- [endif]-->排序 Map

<!-- [if gte vml 1]><v:shape id="_x0000_i1031" type="#_x0000_t75" style="width:235.5pt;height:224.25pt" mce_style="width:235.5pt;height:224.25pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image005.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image005.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

private SortedMap images = new TreeMap ();

< map name = "images" table = "ITEM_IMAGE" sort = "natural" >

           < key column = "ITEM_ID" />

           < map-key type = "string" column = "IMAGENAME" />

           < element type = "string" column = "FILENAME" not-null = "true" />

</ map >

 

 

<!-- [if !supportLists]-->节 1.06   <!-- [endif]-->有序集合 Ordered Collection

<!-- [if !supportLists]-->(a)      <!-- [endif]-->有序 Set

<!-- [if gte vml 1]><v:shape id="_x0000_i1032" type="#_x0000_t75" style="width:197.25pt;height:192pt" mce_style="width:197.25pt;height:192pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image001.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

private Set images = new LinkedHashSet ();

< set name = "images" table = "ITEM_IMAGE" order-by = "ITEM_ID" >

           < key column = "ITEM_ID" />

           < element type = "string" column = "FILENAME" not-null = "true" />

</ set >

<!-- [if !supportLists]-->(b)      <!-- [endif]-->有序 Map

<!-- [if gte vml 1]><v:shape id="_x0000_i1030" type="#_x0000_t75" style="width:235.5pt;height:224.25pt" mce_style="width:235.5pt;height:224.25pt"> <v:imagedata src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image005.png" mce_src="file:///C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp\msohtml1\02\clip_image005.png" o:title="1"/> </v:shape><![endif]--><!-- [if !vml]--><!-- [endif]-->

private Map images = new LinkedHashMap ();

< map name = "images" table = "ITEM_IMAGE" order-by = "IMAGENAME asc" >

           < key column = "ITEM_ID" />

           < map-key type = "string" column = "IMAGENAME" />

           < element type = "string" column = "FILENAME" not-null = "true" />

</ map >

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics