JPA 不为多对多关系创建数据库条目

JPA not creating database entry for many to many relationship

我有这两个 类(缩写):

@Entity
public class ClanPlayer {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private int id;
    @ManyToMany(mappedBy = "players")
    private List<PhoenixPermission> permissions = new ArrayList<>();

    public ClanPlayer() {}

    public List<PhoenixPermission> getPermissions() {
        return permissions;
    }

    public void setPermissions(List<PhoenixPermission> permissions) {
        this.permissions = permissions;
    }

    public void addPermission(PhoenixPermission permission) {
        EntityManager em = EMUtils.getEntityManager();
        em.getTransaction().begin();
        permissions.add(permission);
        em.getTransaction().commit();
    }
}

@Entity
public class PhoenixPermission {
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Id
    private int id;
    @ManyToMany
    @JoinTable(name = "PLAYER_PERMISSION")
    private List<ClanPlayer> players = new ArrayList<>();

    public PhoenixPermission() {}

    public List<ClanPlayer> getPlayers() {
        return players;
    }

    public void setPlayers(List<ClanPlayer> players) {
        this.players = players;
    }
}

现在,在使用 PhoenixPermission-Entity 对象调用 addPermission 方法时,ArrayList 'permissions' 已更新,但 'PLAYER_PERMISSION' [的数据库中没有新的元组 table.

调试日志未显示对此 table 的任何插入查询。 所有其他 Entity Manager 操作都运行良好。 我假设我的注释有问题,但我不确定。 有什么想法吗?

找到了。 我只需要更新这两个对象。这是更新后的 addPermission 方法:

public void addPermission(PhoenixPermission permission) {
    EntityManager em = EMUtils.getEntityManager();
    em.getTransaction().begin();
    permissions.add(permission);
    permission.getPlayers().add(this);
    em.getTransaction().commit();
}