简体   繁体   English

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

[英]Hibernate: Map Two Columns to a HashMap's Key and Value

I have a database with an object, which has translations. 我有一个带有对象的数据库,它有翻译。

There are 2 tables: table Object which has id and more properties and table 'object_translation' which has object_id , language (varchar) and translation (varchar) 有2个表:table具有id和更多属性的对象和具有object_idlanguage (varchar)和translation (varchar)的表'object_translation'

I would like to map this to 我想把这个映射到

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

Where language is an enum in code and a string in the DB. 其中language是代码中的枚举,DB中是字符串。

Is this possible with annotations? 这可能带注释吗? or do I need to create either Collection<QuestionTranslation> or write my own hibernate mapping functions in the DAO (I'm using spring-data, so I would prefer to keep it nice and clean as interfaces and annotations) 或者我是否需要在DAO中创建Collection<QuestionTranslation>或编写我自己的hibernate映射函数(我使用的是spring-data,所以我更喜欢保持它作为接口和注释保持干净整洁)

Versions: spring-boot 2 with provided hibernate from spring-boot-starter: 版本:spring-boot 2,提供spring-boot-starter的休眠:

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>

So far I have this: 到目前为止我有这个:

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

But how do I map the value as well? 但是我如何映射价值呢? (for clarification: the String value should be the translation column in the DB) (澄清:String值应该是DB中的翻译列)

Because I don't have a primary-key, this should theoretically be possible? 因为我没有主键,理论上这应该是可能的吗?

Thanks in advance! 提前致谢!

I managed to fix it =) 我设法修复它=)

You shouldn't use @OneToMany . 你不应该使用@OneToMany

What I used: 我用过的:

public class Object {

    @ElementCollection(fetch = FetchType.EAGER)
    @CollectionTable(name = "object_translation",
                 foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_object_translation_object"),
                 joinColumns = @JoinColumn(name = "object_id"))
    @MapKeyColumn(name = "language", nullable = false)
    @MapKeyEnumerated(EnumType.STRING)
    @Column(name = "translation", nullable = false)
    private Map<Language, String> translations;
}

It is not achievable from ORM perspective. 从ORM的角度来看,这是不可能实现的。 There is no way to tell ORM framework to map String to Translation column. 没有办法告诉ORM框架将String映射到Translation列。

The best way to achieve this would be by retrieving the rows from Object_Translation as a List or Set using @OneToMany or @ManyToMany with mappedBy and then writing your custom to convert them to a map. 实现此目的的最佳方法是使用@OneToMany将Object_Translation中的行作为List或Set检索,或者使用mappedBy检索@ManyToMany,然后编写自定义以将它们转换为映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM