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

后端 未结 1 2016
-上瘾入骨i
-上瘾入骨i 2020-12-19 19:19

I\'m having two entities Car and CarDescription where CarDescription is depending on another foreign key from the table Language

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-19 20:17

    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 carDescription = new HashMap<>(0);
    

    0 讨论(0)
提交回复
热议问题