hibernate中Map类型的双向一对多映射


有两个类:MapLayer和MapLayerField

MapLayerField.java

xml文件
MapLayer.hbm.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--  
  5.     Mapping file autogenerated by MyEclipse Persistence Tools  
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.hgTIS.webgis.model.MapLayer" table="mapLayer" >  
  9.         <id name="id" type="long">  
  10.             <column name="id" />  
  11.             <generator class="native"/>  
  12.         </id>  
  13.         <property name="name" column="name" not-null="true"/>  
  14.         <property name="chName" column="chName"/>  
  15.         <property name="display" column="display"/>  
  16.         <property name="placeOrder" column="placeOrder"/>  
  17.         <property name="minScale" column="minScale"/>  
  18.         <property name="maxScale" column="maxScale"/>  
  19.         <property name="feaType" column="feaType"/>  
  20.         <property name="type" column="type"/>  
  21.         <property name="coordinateSystem" column="coordinateSystem"/>  
  22.         <many-to-one name="parentLayer" column="parentLayerId"/>  
  23.         <property name="sortField" column="sortField"/>  
  24.         <map name="displayFields" inverse="true" cascade="all">  
  25.             <key column="layerid"/>  
  26.             <map-key type="string" column="name"/><!--  上面引用的那篇文章说index标签也可以-->  
  27.             <one-to-many class="com.hgTIS.webgis.model.MapLayerField"/>  
  28.         </map>  
  29.     </class>  
  30. </hibernate-mapping>  

MapLayerField.hbm.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  4. <!--   
  5.     Mapping file autogenerated by MyEclipse Persistence Tools  
  6. -->  
  7. <hibernate-mapping>  
  8.     <class name="com.hgTIS.webgis.model.MapLayerField" table="mapLayerField" >  
  9.         <id name="id" type="long">  
  10.             <column name="id" />  
  11.             <generator class="native" />  
  12.         </id>  
  13.         <property name="name" column="name" not-null="true"/>  
  14.         <property name="chName" column="chName"/>  
  15.         <property name="systemField" column="systemField"/>  
  16.         <property name="viewField" column="viewField"/>  
  17.         <property name="sortField" column="sortField"/>  
  18.         <property name="titleField" column="titleField"/>  
  19.         <property name="forder" column="forder"/>  
  20.         <many-to-one name="mapLayer" column="layerid"/>  
  21.     </class>  
  22. </hibernate-mapping>  

 

主要是映射文件中的map标签中的map-key标签一定要与相应的类中的属性一致!

你可能感兴趣的:(hibernate中Map类型的双向一对多映射)