Search in sources :

Example 1 with EntityViewManager

use of com.blazebit.persistence.view.EntityViewManager in project blaze-persistence by Blazebit.

the class EntityViewSettingTest method testEntityViewSettingCustomRoot.

@Test
public void testEntityViewSettingCustomRoot() {
    EntityViewManager evm = build(CustomRootPersonView.class);
    // Base setting
    EntityViewSetting<CustomRootPersonView, CriteriaBuilder<CustomRootPersonView>> setting = EntityViewSetting.create(CustomRootPersonView.class);
    // Query
    CriteriaBuilder<Document> cb = cbf.create(em, Document.class);
    setting.addAttributeFilter("name", "pers1");
    CriteriaBuilder<CustomRootPersonView> criteriaBuilder = evm.applySetting(setting, cb, "owner");
    assertEquals("SELECT " + singleValuedAssociationIdPath("owner_1.id", "owner_1") + " AS CustomRootPersonView_id, owner_1.name AS CustomRootPersonView_name " + "FROM Document document JOIN document.owner owner_1 " + "WHERE owner_1.name <> :param_0", criteriaBuilder.getQueryString());
    List<CustomRootPersonView> result = criteriaBuilder.getResultList();
    assertEquals(2, result.size());
    assertEquals("pers2", result.get(0).getName());
    assertEquals("pers2", result.get(1).getName());
}
Also used : CriteriaBuilder(com.blazebit.persistence.CriteriaBuilder) PaginatedCriteriaBuilder(com.blazebit.persistence.PaginatedCriteriaBuilder) CustomRootPersonView(com.blazebit.persistence.view.testsuite.basic.model.CustomRootPersonView) EntityViewManager(com.blazebit.persistence.view.EntityViewManager) Document(com.blazebit.persistence.testsuite.entity.Document) FilteredDocument(com.blazebit.persistence.view.testsuite.basic.model.FilteredDocument) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 2 with EntityViewManager

use of com.blazebit.persistence.view.EntityViewManager in project blaze-persistence by Blazebit.

the class AbstractEntityViewTest method build.

public EntityViewManager build(EntityViewConfiguration cfg, Class<?>... classes) {
    for (Class<?> c : classes) {
        cfg.addEntityView(c);
    }
    EntityViewManagerFactoryCacheKey cacheKey = new EntityViewManagerFactoryCacheKey(cbf, cfg);
    EntityViewManager evm;
    if ((evm = evmCache.get(cacheKey)) == null) {
        evm = build0(cfg, classes);
        evmCache.put(cacheKey, evm);
    }
    AbstractEntityViewTest.evm = evm;
    PackageOpener packageOpener = cbf.getService(PackageOpener.class);
    boolean unsafeDisabled = !Boolean.valueOf(String.valueOf(cfg.getProperty(ConfigurationProperties.PROXY_UNSAFE_ALLOWED)));
    boolean strictCascadingCheck = Boolean.valueOf(String.valueOf(cfg.getProperty(ConfigurationProperties.UPDATER_STRICT_CASCADING_CHECK)));
    ProxyFactoryCacheKey proxyFactoryCacheKey = new ProxyFactoryCacheKey(unsafeDisabled, strictCascadingCheck, packageOpener);
    ProxyFactory proxyFactory;
    if ((proxyFactory = proxyFactoryCache.get(proxyFactoryCacheKey)) == null) {
        proxyFactoryCache.put(proxyFactoryCacheKey, ((EntityViewManagerImpl) evm).getProxyFactory());
    } else {
        try {
            Field proxyFactoryField = EntityViewManagerImpl.class.getDeclaredField("proxyFactory");
            proxyFactoryField.setAccessible(true);
            proxyFactoryField.set(evm, proxyFactory);
            boolean scanStaticImplementations = !Boolean.valueOf(String.valueOf(cfg.getProperty(ConfigurationProperties.STATIC_IMPLEMENTATION_SCANNING_DISABLED)));
            for (ManagedViewType<?> managedView : evm.getMetamodel().getManagedViews()) {
                Class<?> javaType = managedView.getJavaType();
                if (!javaType.isInterface() && !Modifier.isAbstract(javaType.getModifiers())) {
                    proxyFactory.setImplementation(javaType);
                } else if (scanStaticImplementations) {
                    proxyFactory.loadImplementation(new HashSet<>(), managedView, evm);
                }
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
    return evm;
}
Also used : ProxyFactory(com.blazebit.persistence.view.impl.proxy.ProxyFactory) Field(java.lang.reflect.Field) EntityViewManager(com.blazebit.persistence.view.EntityViewManager) PackageOpener(com.blazebit.persistence.spi.PackageOpener) HashSet(java.util.HashSet)

Example 3 with EntityViewManager

use of com.blazebit.persistence.view.EntityViewManager in project blaze-persistence by Blazebit.

the class WithCTEMappingTest method testCteBinding.

@Test
public void testCteBinding() {
    EntityViewManager evm = build(DocumentWithCTE.class);
    CriteriaBuilder<Document> cb = cbf.create(em, Document.class).orderByAsc("id");
    EntityViewSetting<DocumentWithCTE, CriteriaBuilder<DocumentWithCTE>> setting;
    setting = EntityViewSetting.create(DocumentWithCTE.class);
    setting.addOptionalParameter("ownerMaxAge", 63L);
    List<DocumentWithCTE> list = evm.applySetting(setting, cb).getResultList();
    assertEquals(2, list.size());
    assertEquals("doc1", list.get(0).getName());
    assertEquals("doc2", list.get(1).getName());
    assertNull(list.get(0).getOwnedDocumentCount());
    assertEquals(Long.valueOf(1), list.get(1).getOwnedDocumentCount());
}
Also used : CriteriaBuilder(com.blazebit.persistence.CriteriaBuilder) EntityViewManager(com.blazebit.persistence.view.EntityViewManager) DocumentWithCTE(com.blazebit.persistence.view.testsuite.cte.model.DocumentWithCTE) Document(com.blazebit.persistence.testsuite.entity.Document) PersonWithPartnerDocument(com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocument) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 4 with EntityViewManager

use of com.blazebit.persistence.view.EntityViewManager in project blaze-persistence by Blazebit.

the class WithCTEMappingTest method testCteBindingWithSubview.

@Test
public void testCteBindingWithSubview() {
    EntityViewManager evm = build(DocumentWithCTE.class, PersonWithPartnerDocument.class, PersonWithPartnerDocumentFullAged.class, PersonWithPartnerDocumentUnderAged.class);
    CriteriaBuilder<Person> cb = cbf.create(em, Person.class).orderByAsc("id");
    EntityViewSetting<PersonWithPartnerDocument, CriteriaBuilder<PersonWithPartnerDocument>> setting;
    setting = EntityViewSetting.create(PersonWithPartnerDocument.class);
    List<PersonWithPartnerDocument> list = evm.applySetting(setting, cb).getResultList();
    assertEquals(3, list.size());
    assertEquals("doc1", list.get(0).getPartnerDocument().getName());
    assertEquals(Long.valueOf(1), list.get(0).getPartnerDocument().getOwnedDocumentCount());
    boolean containsProvider = false;
    ManagedViewType<?> viewType = evm.getMetamodel().managedView(PersonWithPartnerDocumentFullAged.class);
    for (CTEProvider provider : viewType.getCteProviders()) {
        if (provider instanceof FullAgedCTEProvider) {
            containsProvider = true;
        }
    }
    assertTrue(containsProvider);
}
Also used : CriteriaBuilder(com.blazebit.persistence.CriteriaBuilder) FullAgedCTEProvider(com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocumentFullAged.FullAgedCTEProvider) CTEProvider(com.blazebit.persistence.view.CTEProvider) FullAgedCTEProvider(com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocumentFullAged.FullAgedCTEProvider) EntityViewManager(com.blazebit.persistence.view.EntityViewManager) PersonWithPartnerDocument(com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocument) Person(com.blazebit.persistence.testsuite.entity.Person) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 5 with EntityViewManager

use of com.blazebit.persistence.view.EntityViewManager in project blaze-persistence by Blazebit.

the class AttributeFilterTest method testViewFilter.

@Test
public void testViewFilter() {
    EntityViewManager evm = build(ViewFilterPrimitiveDocumentView.class);
    EntityViewSetting<ViewFilterPrimitiveDocumentView, CriteriaBuilder<ViewFilterPrimitiveDocumentView>> setting = EntityViewSetting.create(ViewFilterPrimitiveDocumentView.class);
    setting.addViewFilter("viewFilter1");
    List<ViewFilterPrimitiveDocumentView> results = evm.applySetting(setting, cbf.create(em, PrimitiveDocument.class)).getResultList();
    assertEquals(1, results.size());
    Assert.assertEquals(Long.valueOf(doc1.getId()), results.get(0).getId());
}
Also used : CriteriaBuilder(com.blazebit.persistence.CriteriaBuilder) EntityViewManager(com.blazebit.persistence.view.EntityViewManager) ViewFilterPrimitiveDocumentView(com.blazebit.persistence.view.testsuite.filter.basic.model.ViewFilterPrimitiveDocumentView) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Aggregations

EntityViewManager (com.blazebit.persistence.view.EntityViewManager)70 Test (org.junit.Test)61 AbstractEntityViewTest (com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)51 Document (com.blazebit.persistence.testsuite.entity.Document)31 CriteriaBuilder (com.blazebit.persistence.CriteriaBuilder)25 Category (org.junit.experimental.categories.Category)25 PaginatedCriteriaBuilder (com.blazebit.persistence.PaginatedCriteriaBuilder)19 AbstractCorrelationTest (com.blazebit.persistence.view.testsuite.correlation.AbstractCorrelationTest)7 DocumentForCollections (com.blazebit.persistence.view.testsuite.collections.entity.simple.DocumentForCollections)6 FilteredDocument (com.blazebit.persistence.view.testsuite.basic.model.FilteredDocument)5 Person (com.blazebit.persistence.testsuite.entity.Person)4 DocumentWithEntityView (com.blazebit.persistence.view.testsuite.basic.model.DocumentWithEntityView)4 PersonForCollections (com.blazebit.persistence.view.testsuite.collections.entity.simple.PersonForCollections)4 DocumentSimpleCorrelationViewJoinId (com.blazebit.persistence.view.testsuite.correlation.expression.model.DocumentSimpleCorrelationViewJoinId)4 DocumentMasterView (com.blazebit.persistence.view.testsuite.subview.model.DocumentMasterView)4 EntityViewConfiguration (com.blazebit.persistence.view.spi.EntityViewConfiguration)3 PrimitiveDocument (com.blazebit.persistence.testsuite.entity.PrimitiveDocument)2 PersonWithPartnerDocument (com.blazebit.persistence.view.testsuite.cte.model.PersonWithPartnerDocument)2 HashSet (java.util.HashSet)2 Jsonb (javax.json.bind.Jsonb)2