如何更新分离的实体?

时间:2016-05-31 14:31:33

标签: java spring entity-framework hibernate transactions

我想在@Entity上执行多项更新。但基于一些评价,我必须在两者之间执行长时间运行的任务。任务本身基于该实体的属性。因此,我首先必须获取实体,合并一些内容,运行长任务,然后再次合并。

这将跨越数据库事务,这可能不正确:

@Transactional
public void update(DTO dto) {
    entity = findOne(dto.id);
    updateEntityFieldsFromDTO(entity, dto);

    if (entity.hasTaskCondition) {
        result = runLongTask(entity.property); //spanning the tx
        mergeResultIntoEntity(entity, result);
    }
}

问题:runLongTask()将阻止事务并保持数据库连接打开很长一段时间,而不会在数据库上进行交互。

因此,我想创建单个@Transactional方法,并运行那些tx的任务:

//wrapper method without a transaction
public void updateFacade(DTO dto) {
    entity = service.update(dto.id);

    if (entity.hasTaskCondition) {
        //running outside the transaction
        result = runLongTask(entity.property);
        service.mergeResultIntoEntity(entity.id, result);
    }
}

然后我的交易方法如下:

@Transactional
public Entity update(DTO dto) {
    //first select the entity
    entity = findOne(dto.id);

    //then merge the content
    updateEntityFieldsFromDTO(entity, dto);

    return entity;
}

@Transactional
public Entity mergeResultIntoEntity(id, result) {
    //again select the entity
    entity = findOne(dto.id);

    //now merge result into entity within tx

    return entity;
}

问题:始终将entity.id传递给@Transactional方法,然后再从db 中获取对象是否正确? tx方法?

我认为我无法将实体直接传递给updateFacade的@Transactional方法,因为实体是detached。正确的吗?

0 个答案:

没有答案
相关问题