检索JPA实体时,@ ElementCollection被删除

时间:2019-05-22 06:07:21

标签: hibernate spring-data-jpa

我有一个具有@ElementCollection集合的实体,如下所示

@Entity
public class RecordLog implements Serializable  {
        ......
        ......
    private String component;
    private String entityUuid;
    private Boolean legacy;
    @ElementCollection(fetch=FetchType.EAGER)
    @CollectionTable(name = "auditlog_team_iris")
    @ReadOnlyProperty
    private List<String> teamIris;

       .....
       .....
}

我正在尝试使用功能从RecordLogRepository接口中检索RecordLog实体

@Query("SELECT rl from RecordLog rl where rl.entityUuid = ?1 and rl.component = 'abcd' and rl.legacy is null order by rl.transactionDate desc")
List<RecordLog> getLatestRecordLogByUuid(String entityUuid, Pageable pageable);

我的Service类正在使用以下调用从存储库中检索实体

List<RecordLog> recordLogs = recordLogRepository.getLatestRecordLogByUuid(uuid, PageRequest.of(0, 1));
RecordLog recordLog = recordLogs.get(0);

我没有对检索到的recordLogs进行任何更改,但是当我退出服务类中的函数时,teamIris将从recordLog中删除。

我尝试将@Transactional(readOnly = true)放在服务类中的函数周围以及存储库接口中的函数周围。我也尝试过使用entityManager.detach(recordLog)分离recordLogs;但无济于事。

当我在JPA中启用调试日志时,发现了以下内容。我实际上是在检索一个RecordLog,并保留三个新的RecordLogs。它删除了检索到的RecordLog的teamIris

    27-04-2019 18:08:50.378 [SimpleAsyncTaskExecutor-1] DEBUG o.h.e.t.internal.TransactionImpl.commit - committing
27-04-2019 18:08:50.378 [SimpleAsyncTaskExecutor-1] DEBUG o.h.e.i.AbstractFlushingEventListener.prepareEntityFlushes - Processing flush-time cascades
27-04-2019 18:08:50.379 [SimpleAsyncTaskExecutor-1] DEBUG o.h.e.i.AbstractFlushingEventListener.prepareCollectionFlushes - Dirty checking collections
27-04-2019 18:08:50.380 [SimpleAsyncTaskExecutor-1] DEBUG o.h.engine.internal.Collections.processReachableCollection - Collection found: [com.cybersponse.auditlog.model.RecordLog.teamIris#72], was: [<unreferenced>] (initialized)
27-04-2019 18:08:50.381 [SimpleAsyncTaskExecutor-1] DEBUG o.h.engine.internal.Collections.processReachableCollection - Collection found: [com.cybersponse.auditlog.model.RecordLog.teamIris#73], was: [com.cybersponse.auditlog.model.RecordLog.teamIris#71] (initialized)
27-04-2019 18:08:50.382 [SimpleAsyncTaskExecutor-1] DEBUG o.h.engine.internal.Collections.processReachableCollection - Collection found: [com.cybersponse.auditlog.model.RecordLog.teamIris#74], was: [<unreferenced>] (initialized)
27-04-2019 18:08:50.383 [SimpleAsyncTaskExecutor-1] DEBUG o.h.e.i.AbstractFlushingEventListener.logFlushResults - Flushed: 0 insertions, 0 updates, 0 deletions to 3 objects
27-04-2019 18:08:50.384 [SimpleAsyncTaskExecutor-1] DEBUG o.h.e.i.AbstractFlushingEventListener.logFlushResults - Flushed: 3 (re)creations, 0 updates, 1 removals to 3 collections

1 个答案:

答案 0 :(得分:0)

我本人发现了这个问题,而且有点奇怪。在我的实现中,我定义了另一个名为Lookup的实体,如下所示

@Entity    
public class Lookup {
    ......
    ......
    @ElementCollection(fetch=FetchType.EAGER)
    List<String> teamIris;
}

在我的服务类中,我正在创建一个新的Lookup对象,并从recordLog为其分配teamIris,如下所示:

List<RecordLog> recordLogs = recordLogRepository.getLatestRecordLogByUuid(uuid, PageRequest.of(0, 1));
RecordLog recordLog = recordLogs.get(0);
Lookup lookup = new Lookup();
lookup.setTeamIris(recordLog.getTeamIris());

当我分配了recordLog小组的深层副本时,它可以代替工作,而不是直接将recordLog小组分配给lookup(参考分配)。