use of com.blazebit.persistence.CriteriaBuilder in project blaze-persistence by Blazebit.
the class EntityFetchesTest method entityViewFetchesInitializeEntitiesSimple.
@Test
// NOTE: Datanucleus doesn't seem to support fetching non-root relations
@Category({ NoDatanucleus.class })
public void entityViewFetchesInitializeEntitiesSimple() {
CriteriaBuilder<Person> criteria = cbf.create(em, Person.class).where("id").eq(pers1.getId());
EntityViewSetting<FetchesPersonView1, CriteriaBuilder<FetchesPersonView1>> setting = EntityViewSetting.create(FetchesPersonView1.class);
CriteriaBuilder<FetchesPersonView1> cb = evm.applySetting(setting, criteria);
List<FetchesPersonView1> results = cb.getResultList();
// Close the em to make sure this was fetched properly
em.getTransaction().rollback();
em.close();
// Only a single entity view is produced
assertEquals(1, results.size());
FetchesPersonView1 view = results.get(0);
// Friends match
assertEquals(friend.getId(), view.getFriend().getId());
// Friend is properly initialized
assertEquals(2, view.getFriend().getOwnedDocuments().size());
assertEquals(friend.getOwnedDocuments(), view.getFriend().getOwnedDocuments());
// Owned documents have the correct size
assertEquals(2, view.getOwnedDocuments().size());
// Extract ownedDocuments from view
Iterator<Document> documentIterator = view.getOwnedDocuments().iterator();
Document ownedDoc1 = documentIterator.next();
Document ownedDoc2 = documentIterator.next();
// Extract ownedDocuments from pers1
documentIterator = pers1.getOwnedDocuments().iterator();
Document expectedDoc1 = documentIterator.next();
Document expectedDoc2;
if (!expectedDoc1.getId().equals(ownedDoc1.getId())) {
expectedDoc2 = expectedDoc1;
expectedDoc1 = documentIterator.next();
} else {
expectedDoc2 = documentIterator.next();
}
// Check contacts have been initialized of owned documents
assertEquals(2, ownedDoc1.getContacts().size());
assertEquals(expectedDoc1.getContacts(), ownedDoc1.getContacts());
assertEquals(2, ownedDoc2.getContacts().size());
assertEquals(expectedDoc2.getContacts(), ownedDoc2.getContacts());
// Extract partner documents
Document partnerDocument = pers1.getPartnerDocument();
FetchesDocumentView1 partnerDocumentView = view.getPartnerDocument();
// Check personList is properly initialized
assertEquals(2, partnerDocumentView.getPeople().size());
assertEquals(partnerDocument.getPeople(), partnerDocumentView.getPeople());
assertEquals(pers1, partnerDocumentView.getPeople().get(0));
assertEquals(2, partnerDocumentView.getPeople().get(0).getOwnedDocuments().size());
assertEquals(pers1.getOwnedDocuments(), partnerDocumentView.getPeople().get(0).getOwnedDocuments());
assertEquals(friend, partnerDocumentView.getPeople().get(1));
assertEquals(2, partnerDocumentView.getPeople().get(1).getOwnedDocuments().size());
assertEquals(friend.getOwnedDocuments(), partnerDocumentView.getPeople().get(1).getOwnedDocuments());
}
use of com.blazebit.persistence.CriteriaBuilder in project blaze-persistence by Blazebit.
the class EntityFetchesTest method entityViewFetchesInitializeEntities.
@Test
// NOTE: Datanucleus doesn't seem to support fetching non-root relations
@Category({ NoEclipselink.class, NoDatanucleus.class })
public void entityViewFetchesInitializeEntities() {
CriteriaBuilder<Person> criteria = cbf.create(em, Person.class).where("id").eq(pers1.getId());
EntityViewSetting<FetchesPersonView2, CriteriaBuilder<FetchesPersonView2>> setting = EntityViewSetting.create(FetchesPersonView2.class);
CriteriaBuilder<FetchesPersonView2> cb = evm.applySetting(setting, criteria);
List<FetchesPersonView2> results = cb.getResultList();
// Close the em to make sure this was fetched properly
em.getTransaction().rollback();
em.close();
// Only a single entity view is produced
assertEquals(1, results.size());
FetchesPersonView2 view = results.get(0);
// Friends match
assertEquals(friend.getId(), view.getFriend().getId());
// Friend is properly initialized
assertEquals(2, view.getFriend().getOwnedDocuments().size());
assertEquals(friend.getOwnedDocuments(), view.getFriend().getOwnedDocuments());
// Owned documents have the correct size
assertEquals(2, view.getOwnedDocuments().size());
// Extract ownedDocuments from view
Iterator<Document> documentIterator = view.getOwnedDocuments().iterator();
Document ownedDoc1 = documentIterator.next();
Document ownedDoc2 = documentIterator.next();
// Extract ownedDocuments from pers1
documentIterator = pers1.getOwnedDocuments().iterator();
Document expectedDoc1 = documentIterator.next();
Document expectedDoc2;
if (!expectedDoc1.getId().equals(ownedDoc1.getId())) {
expectedDoc2 = expectedDoc1;
expectedDoc1 = documentIterator.next();
} else {
expectedDoc2 = documentIterator.next();
}
// Check contacts have been initialized of owned documents
assertEquals(2, ownedDoc1.getContacts().size());
assertEquals(expectedDoc1.getContacts(), ownedDoc1.getContacts());
assertEquals(2, ownedDoc2.getContacts().size());
assertEquals(expectedDoc2.getContacts(), ownedDoc2.getContacts());
// Extract partner documents
Document partnerDocument = pers1.getPartnerDocument();
FetchesDocumentView2 partnerDocumentView = view.getPartnerDocument();
// Check contacts and personList are properly initialized
assertEquals(2, partnerDocumentView.getContacts().size());
assertEquals(partnerDocument.getContacts(), partnerDocumentView.getContacts());
assertEquals("p6", partnerDocumentView.getContacts().get(0).getName());
assertEquals(0, partnerDocumentView.getContacts().get(0).getOwnedDocuments().size());
assertEquals("p7", partnerDocumentView.getContacts().get(1).getName());
assertEquals(0, partnerDocumentView.getContacts().get(1).getOwnedDocuments().size());
assertEquals(2, partnerDocumentView.getPeople().size());
assertEquals(partnerDocument.getPeople(), partnerDocumentView.getPeople());
assertEquals(pers1, partnerDocumentView.getPeople().get(0));
assertEquals(2, partnerDocumentView.getPeople().get(0).getOwnedDocuments().size());
assertEquals(pers1.getOwnedDocuments(), partnerDocumentView.getPeople().get(0).getOwnedDocuments());
assertEquals(friend, partnerDocumentView.getPeople().get(1));
assertEquals(2, partnerDocumentView.getPeople().get(1).getOwnedDocuments().size());
assertEquals(friend.getOwnedDocuments(), partnerDocumentView.getPeople().get(1).getOwnedDocuments());
}
use of com.blazebit.persistence.CriteriaBuilder 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());
}
use of com.blazebit.persistence.CriteriaBuilder 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());
}
use of com.blazebit.persistence.CriteriaBuilder 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);
}
Aggregations