@ManyToMany 在保存一个实体列表后刷新,在保存第二个实体列表后不刷新

@ManyToMany after save of one entity list is refreshed after save of second isn't

大家好我的问题很奇怪所以让我给你看部分代码:

AllegroFieldImpl的一部分:

@ManyToMany(targetEntity = AllegroCategoryImpl.class)
@JoinTable(name = "ALLEGRO_CATEGORIES_XREF", joinColumns = { 
        @JoinColumn(name = "ALLEGRO_FIELD_ID", nullable = false, updatable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "ALLEGRO_CATEGORY_ID", 
                nullable = false, updatable = false) })
@JsonBackReference
private List<AllegroCategory> allegroCategory = new ArrayList<AllegroCategory>();

AllegroCategoryImpl的一部分:

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL,targetEntity = AllegroFieldImpl.class)
@JoinTable(name = "ALLEGRO_CATEGORIES_XREF", joinColumns = { 
        @JoinColumn(name = "ALLEGRO_CATEGORY_ID", nullable = false, updatable = false) }, 
        inverseJoinColumns = { @JoinColumn(name = "ALLEGRO_FIELD_ID", 
                nullable = false, updatable = false) })
private List<AllegroField> allegroFields = new ArrayList<AllegroField>();

如您所见,我这里有 2 个具有多对多关系模型的实体。一切都运行良好,列表已获取等。但有一个问题我无法通过。当我创建新的 AllegroFieldImpl 并为其添加现有的 AllegroCategoryImpl 时,一切都很好地保存在数据库中但是当我在 AllegroCategoryImpl getAllegroFields() 中执行方法时,它看起来像没有更新(我必须重新启动服务器 - 也许某些东西存储在 catche 中?)。这是将向您解释的代码示例:

AllegroCategory ac = allegroService.readAllegroCategoryByID(1);
logger.error("Fields size: "+ac.getAllegroFields().size()); // 5

// Creating new AllegroField

AllegroField afnew = new AllegroFieldImpl();
afnew.setAllegroFieldId(421312);
afnew.setName("asdasdasdsa12312312312");
afnew.setObligatory(false);

afnew.getAllegroCategory().add(ac);
em.merge(afnew);

// Checking fields size
logger.error("Fields size: "+ac.getAllegroFields().size()); // 5

有什么办法解决这个问题吗?

检查方法是否有 readAllegroCategoryByID - @Transactional