jmix-framework / jmix

Jmix framework
https://www.jmix.io
Apache License 2.0
696 stars 125 forks source link

EntityManager loads deleted entities if there was lazy loading in the same transaction #3941

Closed knstvk closed 1 day ago

knstvk commented 1 week ago

Environment

Jmix version: 1.6.2

Bug Description

When using the EntityManager to load data, if any field is lazily loaded beforehand, the SOFT_DELETION property is turned off in the EntityManager, and soft deleted entities are loaded.

// Two students are created, (Student has m-to-1 `group` field), one of them has been soft deleted. 

FetchPlan fetchPlan = fetchPlans.builder(Student.class)
        .addFetchPlan(FetchPlan.INSTANCE_NAME)
        .build();

PlatformTransactionManager transactionManager = storeAwareLocator.getTransactionManager(Stores.MAIN);
TransactionStatus tx = transactionManager.getTransaction(new DefaultTransactionDefinition());
try {
    List<Student> studentsBefore = dataManager.load(Student.class)
            .all()
            .fetchPlan(fetchPlan)
            .list();
    System.out.println(String.format("student count before: %s", studentsBefore.size())); //1
    log.info("student count before: {}", studentsBefore.size()); 

    System.out.println(String.format("soft-deletion before lazy loading = %s",
            entityManager.getProperties().get(PersistenceHints.SOFT_DELETION)));  //true
    Group group = studentsBefore.get(0).getGroup();

    System.out.println(String.format("soft-deletion after lazy loading = %s",
            entityManager.getProperties().get(PersistenceHints.SOFT_DELETION)));  //false
    List<Student> studentsAfter = entityManager.createQuery("select e from Student e", Student.class)
            .setHint(PersistenceHints.FETCH_PLAN, fetchPlan)
            .getResultList();
    System.out.println(String.format("student count after: %s", studentsAfter.size()));  //2
    transactionManager.commit(tx);
} catch (Exception e) {
    transactionManager.rollback(tx);
}

QA Notes

Covered by autotest

glebfox commented 5 days ago

Port needed