尽管刷新和事务提交,Hibernate实体属性仍未持久化到数据库

我有一个Entity MXGroup,它可以通过status属性进行逻辑删除.
该实体通过Hibernate持久存储到MySQL数据库中.
当我通过我的JSF页面创建这个实体时,保持它,然后尝试更新该实体的状态列而不在创建之间重新加载我的页面并在逻辑上删除它,状态列的更改不会持久化.

该实体显然位于Hibernate的缓存和数据库中.更新属性然后调用与它的合并不会将更改合并到数据库中.

合并实体后,我在我的实体管理器上显式调用了flush和getTransaction().commit(),但仍然没有骰子.

如果我刷新我的页面(可能获得一个不同的EntityManager,因为我的EntityManagers是对话作用域),那么我可以突然删除我的MXGroup而没有任何问题.

我知道我可以做一些变通办法 – 所以这主要是为什么会发生这种情况的学术练习……

我的MXGroup实体通过ajax通过RESTful接口加载,具有以下方法:( PersonalGroup实体扩展MXGroup实体)

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("personal/")
public Response restPersonalGroups() {
    return Response.ok().entity(serializePersonalGroups()).build();
}

public String serializePersonalGroups() {
    final List<PersonalGroup> personalGroupsList = this.userGroupService.getPersonalGroups();
    String personalGroups = serializeGroupList(personalGroupsList);
    return personalGroups;
}

使用userGroupService.getPersonalGroups()方法,如下所示:

public List<PersonalGroup> getPersonalGroups() {
    CriteriaBuilder builder = this.getEntityManager().getCriteriaBuilder();
    CriteriaQuery<PersonalGroup> criteria = builder.createQuery(PersonalGroup.class);
    Root<PersonalGroup> root = criteria.from(PersonalGroup.class);

    List<Predicate> predicateList = new ArrayList<Predicate>();
    predicateList.addAll(Arrays.asList(getCommonPredicates(PersonalGroup.class, builder, criteria, root)));
    predicateList.add(builder.equal(root.get(PersonalGroup_.user), getUser()));

    TypedQuery<PersonalGroup> query = this.getEntityManager().createQuery(
        criteria.select(root)
            .where(predicateList.toArray(new Predicate[predicateList.size()]))
            .orderBy(builder.asc(root.get(MXGroup_.name)))
            .distinct(true));

    List<PersonalGroup> personalGroups = query.getResultList();
    for (PersonalGroup pg : personalGroups) {
        this.getEntityManager().refresh(pg);
    }
    return personalGroups;
}

getCommonPredicates在这种情况下只添加一个谓词检查status == 1

删除功能如下所示:

public void deletePersonalGroup() throws BadLoginNameException, UniqueUserLoginException {
    String groupIdStr = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("groupId");
    int groupId = Integer.parseInt(groupIdStr);
    MXGroup group = this.userGroupService.findMXGroup(groupId);

    group.setStatus(0);
    this.userGroupService.mergeMXGroup(group);
    this.userGroupService.forceCommit();
}

mergeMXGroup是:

@Override
public void mergeMXGroup(MXGroup group) {
    super.merge(group);
}

其超级实现如下:

protected <T> T merge(T entity) {
    if (canUseService() && beforeMergeEntity(entity)) {
        T merge = this.getEntityManager().merge(entity);
        return merge;
    }
    return null;
}

最后我强制刷新并像这样提交:

public void forceCommit() {
    this.getEntityManager().getTransaction().commit();
    this.getEntityManager().flush();
}    

RESTful接口几乎肯定会有一个与删除代码不同的实体管理器,因为删除代码是CDI管理的,RESTful接口是EJB.但是我会认为flush和commit会解决这个问题吗?事实上,这是我第一次在整个项目中明确刷新/提交……

有什么想法吗?

最佳答案
我正在审查有关合并操作的语义的规范.

这些场景中的任何一个都可以解释您的问题的原因(或者可能不是):

The semantics of the merge operation applied to an entity X are as
follows:

  • If X is a detached entity, the state of X is copied onto a pre-existing managed entity instance X’ of the same identity or a new
    managed copy X’ of X is created.
  • If X is a new entity instance, a new managed entity instance X’ is created and the state of X is copied into the new managed entity instance X’.
  • If X is a removed entity instance, an IllegalArgumentException will be thrown by the merge operation (or the transaction commit will fail).
  • If X is a managed entity, it is ignored by the merge operation, however, the merge operation is cascaded to entities referenced by relationships from X if these relationships have been annotated with the cascade element value cascade=MERGE or cascade=ALL annotation.
  • If X is an entity merged to X’, with a reference to another entity Y, where cascade=MERGE or cascade=ALL is not specified, then navigation of the same association from X’ yields a reference to a managed object Y’ with the same persistent identity as Y

The persistence provider must not merge fields marked LAZY that have not been fetched: it must ignore such fields when merging.

转载注明原文:尽管刷新和事务提交,Hibernate实体属性仍未持久化到数据库 - 代码日志