Hibernate:将两列映射到HashMap的键和值

时间:2018-11-27 12:25:21

标签: java hibernate hashmap annotations mapping

我有一个带有对象的数据库,该对象具有翻译。

有2个表:具有id和更多属性的表Object和具有object_idlanguage(varchar)和translation(varchar)的表'object_translation'

我想将此映射到

public class Object {
    private Map<Language, String> translations
}

语言是代码中的枚举,是数据库中的字符串。

使用注释是否可行?还是我需要创建Collection<QuestionTranslation>或在DAO中编写我自己的休眠映射函数(我使用的是spring-data,所以我宁愿保持它的美观和干净作为接口和注释)

版本:带有spring-boot-starter提供的休眠模式的spring-boot 2:

spring boot: <version>2.0.3.RELEASE</version>
<hibernate.version>5.2.17.Final</hibernate.version>
<hibernate-jpa-2.1-api.version>1.0.2.Final</hibernate-jpa-2.1-api.version>

到目前为止,我有这个:

@OneToMany
@JoinTable(name = "object_translation", joinColumns = {@JoinColumn(name = "object_id", referencedColumnName = "id")})
@MapKey(name = "language")
@MapKeyEnumerated(EnumType.STRING)
private Map<Language, String> translations;

但是我该如何映射值呢? (为澄清起见:“字符串”值应为数据库中的转换列)

因为我没有主键,从理论上讲这应该可行吗?

谢谢!

2 个答案:

答案 0 :(得分:2)

我设法修复了它=)

您不应使用The determinant of the smoothed correlation was zero. This means the objective function is not defined. Chi square is based upon observed residuals. The determinant of the smoothed correlation was zero. This means the objective function is not defined for the null model either. The Chi square is thus based upon observed correlations. The estimated weights for the factor scores are probably incorrect. Try a different factor extraction method. In factor.scores, the correlation matrix is singular, an approximation is used

我用的是

@OneToMany

答案 1 :(得分:0)

从ORM角度来看这是无法实现的。无法告诉ORM框架将String映射到Translation列。

实现此目标的最佳方法是,使用@OneToMany或@ManyToMany并通过mapedBy检索Object_Translation中的行作为列表或集合,然后编写自定义项以将其转换为地图。