Hibernate集合映射:映射<;字符串,Entity2>;实体1中的属性

Hibernate集合映射:映射<;字符串,Entity2>;实体1中的属性,hibernate,hibernate-mapping,many-to-one,Hibernate,Hibernate Mapping,Many To One,我正在尝试使用(旧版)DB表映射以下实体,但映射映射时遇到问题: public class Configuration { private Long configurationId; private String code; private Long index; private Map<String, ConfigurationParam> configurationParams; ... } 如您所见,配置对象包含configurat

我正在尝试使用(旧版)DB表映射以下实体,但映射映射时遇到问题:

public class Configuration {

    private Long configurationId;
    private String code;
    private Long index;

    private Map<String, ConfigurationParam> configurationParams;
    ...
}
如您所见,配置对象包含configurationParam对象的映射。映射的键是ConfigurationParam的code属性。请注意,每个实体的属性代码彼此不相关(旧模式:()

hibernate映射如下所示:

<class name="Configuration" table="CONFIGURATION" dynamic-update="true">
    <id name="configurationId" column="CONFIGURATIONID">
      <generator class="assigned" />
    </id>
    <property name="configurationSetId" column="CONFIGURATIONSETID" /> 
    <property name="code" column="CODE" /> 
    <property name="index" column="INDX" /> 

    <map name="configurationParams" lazy="true" table="CONFIGURATIONPARAM"  fetch="select"  
        batch-sie="10">
        <key column="CONFIGURATIONID" />
        <map-key type="string" column="CODE"/>
        <element type="ConfigurationParam"></element>
    </map>
  </class>
<map name="configurationParams">
    <key column="CONFIGURATIONID" />
    <map-key type="string" column="CODE"/>
    <one-to-many class="ConfigurationParam" />
</map>


但是,参数没有以所需的方式加载,如果我启用了即时获取,或者如果我尝试使用命名查询获取配置并加入fetch configurationParams,也不会加载

为什么会这样?如何正确地映射关联

我已经有了一些想法问题可能是什么这似乎不是问题(至少是单独的):

  • 这两个实体都有一个名为code的属性,可能是Hibernate将错误的属性用作映射键? (当然,它应该使用ConfigurationParam实体的code属性。)

  • ConfigurationParam.code不是唯一的。如您所见,ConfigurationParam的主键是复合键。 但是,由于它是一个由配置维护的映射,其主键是这个组合的第二部分,所以我认为这应该是可以的

  • ConfigurationParam.code已映射为实体本身的属性。可能,那么您不能再将其用作映射键了?但是,我猜,这个Hibernate也会告诉我


  • 我试图在Hibernate文档中找到答案(你知道猫之类的东西)并在这里阅读了许多其他问题,但没有找到这个特定问题的答案

    好的,毕竟它成功了。地图拥有实体的映射必须如下所示:

    <class name="Configuration" table="CONFIGURATION" dynamic-update="true">
        <id name="configurationId" column="CONFIGURATIONID">
          <generator class="assigned" />
        </id>
        <property name="configurationSetId" column="CONFIGURATIONSETID" /> 
        <property name="code" column="CODE" /> 
        <property name="index" column="INDX" /> 
    
        <map name="configurationParams" lazy="true" table="CONFIGURATIONPARAM"  fetch="select"  
            batch-sie="10">
            <key column="CONFIGURATIONID" />
            <map-key type="string" column="CODE"/>
            <element type="ConfigurationParam"></element>
        </map>
      </class>
    
    <map name="configurationParams">
        <key column="CONFIGURATIONID" />
        <map-key type="string" column="CODE"/>
        <one-to-many class="ConfigurationParam" />
    </map>
    
    
    
    元素标记似乎用于基本类型映射(例如映射)。如果您希望映射实体作为映射值,则需要一对多标记

    hibernate文档说明:“对于基本或可嵌入类型的集合,请使用@ElementCollection”(http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html)


    虽然没有详细解释,也没有针对xml映射,但在解决问题后,我是这样读的。

    好的,毕竟它起作用了。地图拥有实体的映射必须是这样的:

    <class name="Configuration" table="CONFIGURATION" dynamic-update="true">
        <id name="configurationId" column="CONFIGURATIONID">
          <generator class="assigned" />
        </id>
        <property name="configurationSetId" column="CONFIGURATIONSETID" /> 
        <property name="code" column="CODE" /> 
        <property name="index" column="INDX" /> 
    
        <map name="configurationParams" lazy="true" table="CONFIGURATIONPARAM"  fetch="select"  
            batch-sie="10">
            <key column="CONFIGURATIONID" />
            <map-key type="string" column="CODE"/>
            <element type="ConfigurationParam"></element>
        </map>
      </class>
    
    <map name="configurationParams">
        <key column="CONFIGURATIONID" />
        <map-key type="string" column="CODE"/>
        <one-to-many class="ConfigurationParam" />
    </map>
    
    
    
    元素标记似乎用于基本类型映射(例如映射)。如果您希望映射实体作为映射值,则需要一对多标记

    hibernate文档说明:“对于基本或可嵌入类型的集合,请使用@ElementCollection”(http://docs.jboss.org/hibernate/core/3.6/reference/en-US/html/collections.html)

    虽然没有详细解释,也没有针对xml映射,但在解决了我的问题后,我是这样读的