与字符串列表的ManyToMany关系?有可能吗?

时间:2019-02-19 10:05:00

标签: java hibernate jpa

我想将字符串标签列表添加到实体对象:

@ManyToMany
private List<String> tags;

是否可以在不创建新实体对象标签{id,tag}的情况下做到这一点?

使用OneToMany注释可以建立@ElementCollection关系。 但是可以创建ManyToMany关系吗?
任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:0)

不可能。

@ElementCollection只能与OneToMany关系一起使用。 https://en.wikibooks.org/wiki/Java_Persistence/ElementCollection

@ElementCollection可以与基本类型或可嵌入类一起使用。 https://docs.oracle.com/javaee/6/api/javax/persistence/ElementCollection.html

@ManyToMany只能在Entity类上使用。参见@ManyToMany annotated list of strings? Is it ever possible?

解决方案是将字符串包装在具有id的实体中。