Search in sources :

Example 16 with Person

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

the class EntityViewUpdateMutableEntityCollectionsTest method testUpdateAddNewEntityToCollection.

@Test
public void testUpdateAddNewEntityToCollection() {
    // Given & When
    final UpdatableDocumentEntityWithCollectionsView docView = addNewEntityToCollection();
    // Then
    // Assert that the document and the people are loaded i.e. a full fetch
    // In addition, the new person is loaded because of the merge invocation
    // Finally the person is persisted and a single relation insert is done
    AssertStatementBuilder builder = assertUnorderedQuerySequence();
    if (isQueryStrategy()) {
        builder.select(Person.class);
        if (isFullMode()) {
            builder.delete(Document.class, "people").insert(Document.class, "people");
        }
    } else {
        fullFetch(builder);
    }
    builder.insert(Person.class);
    if (version || isQueryStrategy() && isFullMode()) {
        builder.update(Document.class);
    }
    builder.insert(Document.class, "people").validate();
    // Unfortunately, even after an update, we have to reload the entity to merge again
    // This time we even have to re-load owned associations because they aren't lazy and could be dirty
    AssertStatementBuilder afterBuilder = assertQueriesAfterUpdate(docView);
    if (isQueryStrategy()) {
        afterBuilder.select(Person.class);
        afterBuilder.select(Person.class);
        if (isFullMode()) {
            afterBuilder.delete(Document.class, "people").insert(Document.class, "people").insert(Document.class, "people");
        }
    } else {
        fullFetch(afterBuilder);
    }
    afterBuilder.assertSelect().fetching(Person.class, "favoriteDocuments").fetching(Document.class).and().assertSelect().fetching(Person.class, "localized").and().select(Document.class);
    if (version || isQueryStrategy() && isFullMode()) {
        afterBuilder.update(Document.class);
    }
    if (doesJpaMergeOfRecentlyPersistedEntityForceUpdate()) {
        afterBuilder.update(Person.class);
    }
    afterBuilder.validate();
    assertEquals(doc1.getPeople(), docView.getPeople());
    Iterator<Person> iter = doc1.getPeople().iterator();
    Person nextPerson = iter.next();
    if (nextPerson.getId().equals(p1.getId())) {
        assertEquals("pers1", nextPerson.getName());
        assertEquals("newPerson", iter.next().getName());
    } else {
        assertEquals("newPerson", nextPerson.getName());
        assertEquals("pers1", iter.next().getName());
    }
}
Also used : UpdatableDocumentEntityWithCollectionsView(com.blazebit.persistence.view.testsuite.update.entity.mutable.model.UpdatableDocumentEntityWithCollectionsView) AssertStatementBuilder(com.blazebit.persistence.testsuite.base.jpa.assertion.AssertStatementBuilder) Document(com.blazebit.persistence.testsuite.entity.Document) Person(com.blazebit.persistence.testsuite.entity.Person) Test(org.junit.Test) AbstractEntityViewUpdateEntityCollectionsTest(com.blazebit.persistence.view.testsuite.update.entity.AbstractEntityViewUpdateEntityCollectionsTest)

Example 17 with Person

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

the class EntityViewUpdateMutableEntityMapsTest method testUpdateModifyEntityInCollection.

@Test
public void testUpdateModifyEntityInCollection() {
    // Given & When
    final UpdatableDocumentEntityWithMapsView docView = modifyEntityInCollection();
    // Then
    // Assert that the document and the people are loaded i.e. a full fetch
    // Since only an existing person was update, only a single update is generated
    AssertStatementBuilder builder = assertUnorderedQuerySequence();
    if (isQueryStrategy()) {
        builder.select(Person.class);
        if (isFullMode()) {
            builder.delete(Document.class, "contacts").insert(Document.class, "contacts");
        }
    } else {
        fullFetch(builder);
    }
    if (version || isQueryStrategy() && isFullMode()) {
        builder.update(Document.class);
    }
    builder.update(Person.class).validate();
    // Unfortunately, even after an update, we have to reload the entity to merge again
    AssertStatementBuilder afterBuilder = assertQueriesAfterUpdate(docView);
    if (isQueryStrategy()) {
        afterBuilder.select(Person.class);
        if (isFullMode()) {
            afterBuilder.delete(Document.class, "contacts").insert(Document.class, "contacts");
        }
    } else {
        fullFetch(afterBuilder);
    }
    if (version || isQueryStrategy() && isFullMode()) {
        afterBuilder.update(Document.class);
    }
    afterBuilder.validate();
    assertEquals(doc1.getContacts(), docView.getContacts());
    assertEquals("newPerson", p1.getName());
}
Also used : AssertStatementBuilder(com.blazebit.persistence.testsuite.base.jpa.assertion.AssertStatementBuilder) UpdatableDocumentEntityWithMapsView(com.blazebit.persistence.view.testsuite.update.entity.mutable.model.UpdatableDocumentEntityWithMapsView) Document(com.blazebit.persistence.testsuite.entity.Document) Person(com.blazebit.persistence.testsuite.entity.Person) Test(org.junit.Test) AbstractEntityViewUpdateEntityMapsTest(com.blazebit.persistence.view.testsuite.update.entity.AbstractEntityViewUpdateEntityMapsTest)

Example 18 with Person

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

the class SingletonCollectionsTest method setUpOnce.

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

        @Override
        public void work(EntityManager em) {
            doc1 = new Document("doc1");
            doc2 = new Document("doc2");
            doc3 = new Document("doc3");
            doc4 = new Document("doc4");
            Person o1 = new Person("pers1");
            Person o2 = new Person("pers2");
            Person o3 = new Person("pers3");
            doc1.setOwner(o1);
            doc2.setOwner(o2);
            doc3.setOwner(o2);
            doc4.setOwner(o2);
            em.persist(o1);
            em.persist(o2);
            em.persist(o3);
            em.persist(doc1);
            em.persist(doc2);
            em.persist(doc3);
            em.persist(doc4);
        }
    });
}
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)

Example 19 with Person

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

the class MappingIndexTest method setUpOnce.

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

        @Override
        public void work(EntityManager em) {
            Person p = new Person("p");
            doc1 = new Document("doc1", p, new Version(2), new Version(1));
            doc2 = new Document("doc2", p);
            em.persist(doc1);
            em.persist(doc2);
        }
    });
}
Also used : EntityManager(javax.persistence.EntityManager) TxVoidWork(com.blazebit.persistence.testsuite.tx.TxVoidWork) Version(com.blazebit.persistence.testsuite.entity.Version) Document(com.blazebit.persistence.testsuite.entity.Document) Person(com.blazebit.persistence.testsuite.entity.Person)

Example 20 with Person

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

the class ProxyFactoryTest method testClassProxy.

@Test
public void testClassProxy() throws Exception {
    ViewType<DocumentClassView> viewType = getViewMetamodel().view(DocumentClassView.class);
    Class<? extends DocumentClassView> proxyClass = proxyFactory.getProxy(evm, (ManagedViewTypeImplementor<DocumentClassView>) viewType);
    // The parameter order is _id, contacts, firstContactPerson, id, name
    Constructor<? extends DocumentClassView> constructor = proxyClass.getConstructor(Long.class, Map.class, Person.class, Person.class, String.class, Long.class, Integer.class);
    Map<Integer, Person> expectedContacts = new HashMap<Integer, Person>();
    Person expectedFirstContactPerson = new Person("pers");
    Long expectedId = 1L;
    String expectedName = "doc";
    long expectedAge = 10;
    Person expectedMyContactPerson = new Person("my-pers");
    Integer expectedContactPersonNumber = 2;
    DocumentClassView instance = constructor.newInstance(expectedId, expectedContacts, expectedFirstContactPerson, expectedMyContactPerson, expectedName, expectedAge, expectedContactPersonNumber);
    assertTrue(expectedContacts == instance.getContacts());
    assertTrue(expectedFirstContactPerson == instance.getFirstContactPerson());
    assertTrue(expectedId == instance.getId());
    assertTrue(expectedMyContactPerson == instance.getMyContactPerson());
    assertTrue(expectedName == instance.getName());
    assertTrue(expectedAge == instance.getAge());
    assertTrue(expectedContactPersonNumber == instance.getContactPersonNumber());
    expectedContacts = new HashMap<Integer, Person>();
    expectedId = 2L;
    instance.setContacts(expectedContacts);
    instance.setId(expectedId);
    assertTrue(expectedContacts == instance.getContacts());
    assertTrue(expectedId == instance.getId());
}
Also used : UnsafeDocumentClassView(com.blazebit.persistence.view.testsuite.proxy.model.UnsafeDocumentClassView) DocumentClassView(com.blazebit.persistence.view.testsuite.proxy.model.DocumentClassView) HashMap(java.util.HashMap) 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