Search in sources :

Example 1 with Person

use of com.blazebit.persistence.testsuite.entity.Person in project blaze-persistence by Blazebit.

the class AbstractClassViewTest method setUp.

@Before
public void setUp() {
    cleanDatabase();
    transactional(new TxVoidWork() {

        @Override
        public void work(EntityManager em) {
            doc1 = new Document("doc1");
            doc2 = new Document("doc2");
            Person o1 = new Person("pers1");
            Person o2 = new Person("pers2");
            o1.getLocalized().put(1, "localized1");
            o2.getLocalized().put(1, "localized2");
            o1.setPartnerDocument(doc1);
            o2.setPartnerDocument(doc2);
            doc1.setAge(10);
            doc1.setOwner(o1);
            doc2.setAge(20);
            doc2.setOwner(o2);
            doc1.getContacts().put(1, o1);
            doc2.getContacts().put(1, o2);
            doc1.getContacts2().put(2, o1);
            doc2.getContacts2().put(2, o2);
            em.persist(o1);
            em.persist(o2);
            em.persist(doc1);
            em.persist(doc2);
        }
    });
    doc1 = em.find(Document.class, doc1.getId());
    doc2 = em.find(Document.class, doc2.getId());
}
Also used : EntityManager(javax.persistence.EntityManager) TxVoidWork(com.blazebit.persistence.testsuite.tx.TxVoidWork) Document(com.blazebit.persistence.testsuite.entity.Document) Person(com.blazebit.persistence.testsuite.entity.Person) Before(org.junit.Before)

Example 2 with Person

use of com.blazebit.persistence.testsuite.entity.Person 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());
}
Also used : CriteriaBuilder(com.blazebit.persistence.CriteriaBuilder) FetchesPersonView1(com.blazebit.persistence.view.testsuite.basic.model.FetchesPersonView1) Document(com.blazebit.persistence.testsuite.entity.Document) Person(com.blazebit.persistence.testsuite.entity.Person) FetchesDocumentView1(com.blazebit.persistence.view.testsuite.basic.model.FetchesDocumentView1) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 3 with Person

use of com.blazebit.persistence.testsuite.entity.Person 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());
}
Also used : CriteriaBuilder(com.blazebit.persistence.CriteriaBuilder) FetchesPersonView2(com.blazebit.persistence.view.testsuite.basic.model.FetchesPersonView2) Document(com.blazebit.persistence.testsuite.entity.Document) FetchesDocumentView2(com.blazebit.persistence.view.testsuite.basic.model.FetchesDocumentView2) Person(com.blazebit.persistence.testsuite.entity.Person) Category(org.junit.experimental.categories.Category) Test(org.junit.Test) AbstractEntityViewTest(com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)

Example 4 with Person

use of com.blazebit.persistence.testsuite.entity.Person in project blaze-persistence by Blazebit.

the class EntityViewSettingTest method setUpOnce.

@Override
public void setUpOnce() {
    cleanDatabase();
    transactional(new TxVoidWork() {

        @Override
        public void work(EntityManager em) {
            Document doc1 = new Document("MyTest");
            Document doc2 = new Document("YourTest");
            Document doc3 = new Document("NoContacts");
            Person o1 = new Person("pers1");
            Person o2 = new Person("pers2");
            Person o3 = new Person("pers3");
            Person o4 = new Person("pers4");
            Person o5 = new Person("pers5");
            Person o6 = new Person("pers6");
            o1.getLocalized().put(1, "localized1");
            o2.getLocalized().put(1, "localized2");
            o1.setPartnerDocument(doc1);
            o2.setPartnerDocument(doc2);
            doc1.setOwner(o1);
            doc2.setOwner(o2);
            doc3.setOwner(o2);
            doc1.getContacts().put(1, o1);
            doc2.getContacts().put(1, o2);
            doc3.getContacts().put(1, o3);
            doc1.getContacts().put(2, o4);
            doc2.getContacts().put(2, o5);
            doc3.getContacts().put(2, o6);
            doc1.getContacts2().put(2, o1);
            doc2.getContacts2().put(2, o2);
            doc3.getContacts2().put(2, o3);
            em.persist(o1);
            em.persist(o2);
            em.persist(o3);
            em.persist(o4);
            em.persist(o5);
            em.persist(o6);
            em.persist(doc1);
            em.persist(doc2);
            em.persist(doc3);
        }
    });
}
Also used : EntityManager(javax.persistence.EntityManager) TxVoidWork(com.blazebit.persistence.testsuite.tx.TxVoidWork) Document(com.blazebit.persistence.testsuite.entity.Document) FilteredDocument(com.blazebit.persistence.view.testsuite.basic.model.FilteredDocument) Person(com.blazebit.persistence.testsuite.entity.Person)

Example 5 with Person

use of com.blazebit.persistence.testsuite.entity.Person 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)

Aggregations

Person (com.blazebit.persistence.testsuite.entity.Person)175 Document (com.blazebit.persistence.testsuite.entity.Document)94 EntityManager (javax.persistence.EntityManager)77 TxVoidWork (com.blazebit.persistence.testsuite.tx.TxVoidWork)76 Test (org.junit.Test)50 AssertStatementBuilder (com.blazebit.persistence.testsuite.base.jpa.assertion.AssertStatementBuilder)19 Before (org.junit.Before)19 Version (com.blazebit.persistence.testsuite.entity.Version)18 AbstractEntityViewTest (com.blazebit.persistence.view.testsuite.AbstractEntityViewTest)14 HashMap (java.util.HashMap)14 Map (java.util.Map)14 AbstractEntityViewUpdateDocumentTest (com.blazebit.persistence.view.testsuite.update.AbstractEntityViewUpdateDocumentTest)11 Category (org.junit.experimental.categories.Category)11 CriteriaBuilder (com.blazebit.persistence.CriteriaBuilder)10 UpdatablePersonView (com.blazebit.persistence.view.testsuite.update.subview.simple.mutable.model.UpdatablePersonView)7 NameObject (com.blazebit.persistence.testsuite.entity.NameObject)5 Tuple (javax.persistence.Tuple)5 AbstractCoreTest (com.blazebit.persistence.testsuite.AbstractCoreTest)4 NameObjectContainer (com.blazebit.persistence.testsuite.entity.NameObjectContainer)4 QDocument (com.blazebit.persistence.testsuite.entity.QDocument)4