如何删除休眠中的更改检查

WBL*_*ord 7 java spring hibernate hibernate-entitymanager spring-data

Hibernate 在提交事务时检查实体的状态。当获取大量数据发送到客户端时,这是无用的并且对性能至关重要。

我找到了一个解决方案

 entityManager.setFlushMode(FlushModeType.COMMIT);
Run Code Online (Sandbox Code Playgroud)

auto我在查询后放回去。是否有任何正常的更紧凑的解决方案。

现在我正在考虑做一些类似于aspectJ的事情,这样我就不会弄乱代码,并且可以对服务和存储库进行注释。您对此有何看法以及如何解决这样的问题?

例子:

@Override
public Collection<ZoomCallInfoDTO> findZoomCalls(Collection<ContactId> invitedUsers, String name, Date startTime, Date endTime, int offset, int limit, boolean asc, callId callId)
{
    // Why we did it?
    // Due to bad logic of finding calls in time interval (ZoomCallRepository#findAllByProfileAndcallWithinRange)
    // which loaded all calls for user to compute periodical calls there are TOO many entities in hibernate.
    // So during converting calls to DTOs we also have N x M queries (N - number of call to return, M - number of queries for each call conversion).
    // Each query makes hibernate checks all loaded entities for autoFlush policy. And we have bad performance ((
    // Possible decisions:
    // 1. Switch off autoFlush policy for GET methods:
    //    1.1 - Use readOnly transaction (not good for us now because stating top transaction logic is not managed)
    //    1.2 - Set flushMode manually (not good as persistence decision but the cheapest now) - CURRENTLY USED
    // 2. Decrease number of loaded to hibernate entities - it need to redesign logic of computing periodical calls
    // 3. Merge of 1 and 2 decisions - SHOULD BE IMPLEMENTED IN FUTURE
    entityManager.setFlushMode(FlushModeType.COMMIT);

    if (invitedUsers != null && !invitedUsers.isEmpty())
    {
        throw new RuntimeException("Search by invited users is not supported.");
    }

    UserProfile profile = callUtil.getCurrentUserProfile();

    List<ZoomCallInfoDTO> callDTOs = new ArrayList<>();
    Map<callId, callCacheItem> callCache = new HashMap<>();
    for (ZoomCall call : ZoomCallRepository.findAllByProfileAndcallWithinRange(profile.getId(), name, startTime, endTime, offset, limit, asc, callId))
    {
        callDTOs.add(create(call, profile.getId(), callCache));
    }

    entityManager.setFlushMode(FlushModeType.AUTO);

    return callDTOs;
}
Run Code Online (Sandbox Code Playgroud)

我注意到在这样的操作之后有一个“自动刷新”,其中有太多

Esk*_*ini 4

一种解决方案正在实施CustomEntityDirtinessStrategy

properties.setProperty("hibernate.entity_dirtiness_strategy", EntityDirtinessStrategy.class.getName());
Run Code Online (Sandbox Code Playgroud)

详情见:

如何定制Hibernate脏检查机制

更多内容请参见:

实体脏污检查选项