JPA。多对多创建重复的行

JPA. Many to Many creates duplicated rows

请帮助我了解我错在哪里。 我有三个 table:

Table 祝愿

CREATE TABLE WISHES(
  wish_id bigint default nextval('wish_id_inc'::regclass),
  target text not null,
  PRIMARY KEY(wish_id)
)

Table 标签

CREATE TABLE TAGS(
  tag character varying(255) not null,
  PRIMARY KEY(tag)
)

Table Wish_tags

CREATE TABLE wish_tags(
  wish_tags bigint default nextval('wish_tags_id_inc'::regclass),
  wish_id bigint references wishes(wish_id),
  tag_id character varying(255) references tags(tag),
  PRIMARY KEY(wish_tags)
)

我为这些 table 创建了两个 类:

@Entity
@Table(name="wishes")
public class Wish  implements Serializable{
...
      @ManyToMany
      @JoinTable(
          name="wish_tags",
          joinColumns={@JoinColumn(name="wish_id", referencedColumnName="wish_id")},
          inverseJoinColumns={@JoinColumn(name="tag_id", referencedColumnName="tag")})
    private List<Tag> tags;
...
}
    @Entity
    @Table(name="tags")
    public class Tag implements Serializable{
        ...
        @ManyToMany(mappedBy="tags")
        private List<Wish> whishes;
        ...
    }

当我尝试用标签创建愿望时,我得到了 wish_tags table 的副本。

@Transactional
public Wish createWish(List<String> tags){
//em is EntityManager
        ArrayList<Tag> ObTags = new ArraList<Tag>();
        for(String tagId: tags){
            Tag tag = new Tag(tagId);
            ObTags.add(em.merge(tag));
        }
        Wish wish = new Wish(args1,..., ObTags);
        em.persist(wish);
    }

我做错了什么?为什么创建重复项?请帮帮我。

DB 中保存了两个标签,因为一个保存在 EntityManager.merge(tag) 上,另一个保存在级联 EntityManager.persist(wish)ObTags 集合上。

对不起各位,这是我的代码错误。我将相同的标签两次放入愿望中。