1

Is it possible to map an map collection, which use key from referenced table? Its hard for explaining, so here is an example of what I want to accomplish. I want to have map in Type entity from language code (String) to TypeLang entity.

@Entity
public class Type {
    @OneToMany(mappedBy = "type")
    @MapKeyJoinColumn(name = "language.code")
    private Map<String, TypeLang> langMap;
}

@Entity
public class TypeLang {
    @ManyToOne
    @JoinColumn(name = "type_id")
    private Type type;

    @ManyToOne
    @JoinColumn(name = "lang_code")
    private Language language;
}

@Entity
public class Language {
    @Id
    @Column(name = "code")
    @GeneratedValue
    private String code;
}
Tuom
  • 596
  • 5
  • 19

1 Answers1

1

MapKeyJoinColumn is used if you want your Key to be an Entity. To use String you have to use the MapKeyColumn annotation.

shalama
  • 1,133
  • 1
  • 11
  • 25