发生此异常后,所有其他请求都失败“具有相同标识符值的另一个对象已与会话关联”

问题描述 投票:0回答:1

发生此异常后,所有其他请求都失败 - “具有相同标识符值的另一个对象已与会话关联”。

  • javax.persistence.EntityExistsException:具有相同标识符值的另一个对象已与会话关联:[com.dventus.common.user.User#rp_test_user1] 2019-03-21 10:56:44,370 19749 [http-nio- 8080-exec-24] INFO com.atomikos.jdbc.AtomikosConnectionProxy - 用于com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper@7fc183ea的atomikos连接代理:调用rollback ... 2019-03-21 10:56:44,370 19749 [ http-nio-8080-exec-24] INFO com.atomikos.jdbc.AtomikosConnectionProxy - 用于com.mysql.jdbc.jdbc2.optional.JDBC4ConnectionWrapper@7fc183ea的atomikos连接代理:调用setAutoCommit ...

添加我的实体的代码:

public MDMResponse process(MDMRequest mdmRequest)  throws Exception {
    MDMResponse mdmResponse = null;
    AddProcessor.logger.info("\n Request recieved "+mdmRequest.toString());
    final EntityManager entityManager = this.getContext().getEntityManager();
    try {
        final Object entity = this.getContext().getMapper().toEntity(mdmRequest);
        AddProcessor.logger.debug("The entity found is "+entity.getClass().getName());
        entityManager.persist(entity);
        mdmResponse = new MDMResponse(ResponseStatus.OK, Messages.SAVE_SUCCESS);
        AddProcessor.logger.debug(mdmResponse.toString());
    }

    catch (final Exception e) {
        AddProcessor.logger.error(e);
        mdmResponse = new MDMErrorResponse(ResponseStatus.ERROR, Messages.SAVE_ERROR,Catagory.SAVE,Code.ONE,Severity.normal);
    }

    return mdmResponse;
}
java hibernate
1个回答
0
投票

在catch和check之后添加此代码

    finally {
        entityManager.close();
    }
© www.soinside.com 2019 - 2024. All rights reserved.