Hibernate集合映射:映射<;字符串,Entity2>;实体1中的属性
我正在尝试使用(旧版)DB表映射以下实体,但映射映射时遇到问题: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
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,也不会加载
为什么会这样?如何正确地映射关联
我已经有了一些想法问题可能是什么这似乎不是问题(至少是单独的):
我试图在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映射,但在解决了我的问题后,我是这样读的