How to use a child association property as a Map key in JPA parent entity

拈花ヽ惹草 提交于 2019-11-29 12:08:37

In CarDescription you need to add the languageId property:

@Column(name = "language_id", insertable = false, updatable = false)
private Long languageId;

@NotNull
@OneToOne
@JoinColumn(name = "language_id")
private Language language;

public void setLanguage(Language language) {
    this.languageId = language.getId();
    this.language = language;
} 

Then you can use it in the Car entity like this:

@OneToMany(mappedBy="car")
@MapKey(name = "languageId")
private Map<Long, CarDescription> carDescription = new HashMap<>(0);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!