我正在尝试设置Hibernate XML文件,以便以字符串值而不是类实例的形式访问枚举。
DB模式:
table MyEntity (EntityId, EnumerationId)
table MyEnumeration (EnumerationId, EnumerationValue)这提供了多个MyEntity行到一个MyEnumeration行的映射。MyEnumeration的一个例子可能是国家。
Hibernate hbm2hbmxml生成:
<hibernate-mapping>
<class name="MyEntity" table="MyEntity">
<many-to-one name="myEnumeration" class="MyEnumeration" fetch="select">
<column name="EnumerationId" length="36" />
</many-to-one>
...
</hibernate-mapping>上面的映射起作用,因为我现在可以访问代码中的MyEnumeration实例,然后获取EnumerationValue。然而,我想抽象一下这一点。相反,我希望以字符串的形式访问myEnumeration属性,而不是MyEnumeration类。
如何使用Hibernate映射文件完成此操作?
发布于 2011-08-18 16:31:55
@Transient
public String getMyEnumerationValue() {
if (this.myEnumeration == null) {
return null;
}
else {
return this.myEnumeration.getValue();
}
}setter更复杂,因为它需要访问会话才能获得具有给定值的枚举ID (前提是唯一的),以便填充myEnumeration字段。
https://stackoverflow.com/questions/7110540
复制相似问题