From 3196705b589ef9411c6ee9bbb6bb0a62f301b1d1 Mon Sep 17 00:00:00 2001 From: Eugeny Degtyarjov Date: Wed, 25 Nov 2015 12:54:45 +0000 Subject: [PATCH] PL-6340 PersistenceHelper.isLoaded method can initiate unnecessary collection/attribute fetch #PL-6340 --- .../PersistenceAttributeLoadedCheckTest.java | 25 ++++++++++++------- ...GlobalPersistentAttributesLoadChecker.java | 17 +++++++++---- 2 files changed, 28 insertions(+), 14 deletions(-) diff --git a/modules/core/test/com/haulmont/cuba/core/PersistenceAttributeLoadedCheckTest.java b/modules/core/test/com/haulmont/cuba/core/PersistenceAttributeLoadedCheckTest.java index 9391362b76..b21dec2d91 100644 --- a/modules/core/test/com/haulmont/cuba/core/PersistenceAttributeLoadedCheckTest.java +++ b/modules/core/test/com/haulmont/cuba/core/PersistenceAttributeLoadedCheckTest.java @@ -10,28 +10,35 @@ import com.haulmont.cuba.core.global.*; import com.haulmont.cuba.security.entity.Group; import com.haulmont.cuba.security.entity.User; import com.haulmont.cuba.security.entity.UserRole; +import com.haulmont.cuba.testsupport.TestContainer; import com.haulmont.cuba.testsupport.TestSupport; -import org.junit.Assert; +import org.junit.*; import java.util.UUID; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + /** * @author degtyarjov * @version $Id$ */ -public class PersistenceAttributeLoadedCheckTest extends CubaTestCase { +public class PersistenceAttributeLoadedCheckTest { + @ClassRule + public static final TestContainer container = TestContainer.Common.INSTANCE; private DataManager dataManager; + private Persistence persistence; private UUID taskId; private UUID userId; private UUID groupId = UUID.fromString("0fa2b1a5-1d68-4d69-9fbd-dff348347f93"); private View taskView; private View userView; - @Override + @Before public void setUp() throws Exception { - super.setUp(); dataManager = AppBeans.get(DataManager.class); + persistence = AppBeans.get(Persistence.class); try (Transaction tx = persistence.createTransaction()) { EntityManager em = persistence.getEntityManager(); @@ -66,13 +73,13 @@ public class PersistenceAttributeLoadedCheckTest extends CubaTestCase { .addProperty("userRoles", new View(UserRole.class)); } - @Override - protected void tearDown() throws Exception { - deleteRecord("SEC_USER", userId); - deleteRecord("SYS_SCHEDULED_TASK", taskId); - super.tearDown(); + @After + public void tearDown() throws Exception { + container.deleteRecord("SEC_USER", userId); + container.deleteRecord("SYS_SCHEDULED_TASK", taskId); } + @Test public void testIsLoadedLogic() throws Exception { LoadContext userContext = LoadContext.create(User.class).setId(userId).setView(userView); LoadContext taskContext = LoadContext.create(ScheduledTask.class).setId(taskId).setView(taskView); diff --git a/modules/global/src/com/haulmont/cuba/core/global/GlobalPersistentAttributesLoadChecker.java b/modules/global/src/com/haulmont/cuba/core/global/GlobalPersistentAttributesLoadChecker.java index 5169cd71a7..63767d7cbc 100644 --- a/modules/global/src/com/haulmont/cuba/core/global/GlobalPersistentAttributesLoadChecker.java +++ b/modules/global/src/com/haulmont/cuba/core/global/GlobalPersistentAttributesLoadChecker.java @@ -46,11 +46,18 @@ public class GlobalPersistentAttributesLoadChecker implements PersistentAttribut } protected Boolean isLoadedCommonCheck(Object entity, String property) { - if (entity instanceof BaseGenericIdEntity - && ((BaseGenericIdEntity) entity).__inaccessibleAttributes() != null) { - for (String inaccessibleAttr : ((BaseGenericIdEntity) entity).__inaccessibleAttributes()) { - if (inaccessibleAttr.equals(property)) - return false; + if (entity instanceof BaseGenericIdEntity) { + BaseGenericIdEntity baseGenericIdEntity = (BaseGenericIdEntity) entity; + + if (baseGenericIdEntity.__new()) { + return true; + } + + if (baseGenericIdEntity.__inaccessibleAttributes() != null) { + for (String inaccessibleAttr : baseGenericIdEntity.__inaccessibleAttributes()) { + if (inaccessibleAttr.equals(property)) + return false; + } } }