我有 2 个实体/表。

一个是一个适当的实体,我们称之为 data. 。它有许多包含所谓“多语言代码”的字段。

第二个表, code, ,包含多语言值本身。

这是一些示例数据:

Data table
id  name         continentCode  countryCode 
------------------------------------
1   Toto         EU             CH
2   Titi         AS             CN

Code table
id  code   language  text
----------------------------
1   EU     EN        Europe
2   EU     FR        Europe
3   EU     DE        Europa
4   CH     EN        Switzerland
5   CH     FR        Suisse
6   CH     DE        Schweiz
... etc

我想将数据实体中的属性大陆、国家等映射为地图,如下所示:

@OneToMany()
@MapKey(name="languageCode")
private Map<String, Code> continents;

这样我就可以阅读正确语言的文本,如下所示:

Data toto = dao.findByName("Toto");
String text = toto.getContries.get("FR").getText(); //--> returns "Suisse"
String text = toto.getContries.get("EN").getText(); //--> returns "Switzerland"

我还需要能够使用用户的语言对这些“代码”的值进行文本搜索。(例如。获取所有国家='suisse'的数据,法语!)

那么,是否可以绘制一个 OneToMany 使用不是当前实体主键的键字段的集合?我需要我的大陆集合“代码表中的所有记录,其中代码=我的值 continentCode 财产”。或者也许有更合适的方式来表示这种关系?

注意:不幸的是我无法更改 SQL 架构...

有帮助吗?

解决方案

好的,我找到了解决方案。我的数据实体上的 OneToMany 映射如下所示:

@OneToMany()
@JoinColumn(name="code", referencedColumnName="continentCode", insertable=false, updatable=false)   
@MapKey(name="languageCode")
private Map<String, AAGeoContinentThesaurusEntry> continents;

我还必须映射 ContinentalCode 列才能使其正常工作。我没有这样做,我得到了:

org.hibernate.MappingException: Unable to find column with logical name: GCH_CDE_CODE_CNT in org.hibernate.mapping.Table
(GCHDATA) and its related supertables and secondary tables
        at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:396)
        at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:102)

现在我可以做:

myData.getContinentCodes().get("FR").getText() 

并接收字符串“Europe”:)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top