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

后端 未结 1 2012
-上瘾入骨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<Long, CarDescription> carDescription = new HashMap<>(0);
    
    0 讨论(0)
提交回复
热议问题