Search in sources :

Example 1 with SetParent

use of com.vladmihalcea.hibernate.model.eagerset.SetParent in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateSaveIdOnlyEntityTest method test.

@Test
public void test() {
    final Long parentId = cleanAndSaveParent();
    SetParent parent = transactionTemplate.execute(new TransactionCallback<SetParent>() {

        @Override
        public SetParent doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = loadParent(parentId);
            SetChild child1 = new SetChild();
            child1.setName("child1");
            SetChild child2 = new SetChild();
            child2.setName("child2");
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            return parent;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            SetParent idOnlyParent = new SetParent();
            idOnlyParent.setId(parentId);
            SetChild newChild = new SetChild();
            newChild.setName("new");
            newChild.setParent(idOnlyParent);
            entityManager.persist(newChild);
            entityManager.flush();
            SetChild otherChild = new SetChild();
            otherChild.setName("Other");
            idOnlyParent.addChild(otherChild);
            entityManager.flush();
            assertEquals(1, idOnlyParent.getChildren().size());
            return null;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = entityManager.find(SetParent.class, parentId);
            assertEquals(3, parent.getChildren().size());
            return null;
        }
    });
}
Also used : SetChild(com.vladmihalcea.hibernate.model.eagerset.SetChild) SetParent(com.vladmihalcea.hibernate.model.eagerset.SetParent) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 2 with SetParent

use of com.vladmihalcea.hibernate.model.eagerset.SetParent in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateEagerSetTest method test.

@Test
public void test() {
    final Long parentId = cleanAndSaveParent();
    SetParent parent = transactionTemplate.execute(new TransactionCallback<SetParent>() {

        @Override
        public SetParent doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = loadParent(parentId);
            SetChild child1 = new SetChild();
            child1.setName("child1");
            SetChild child2 = new SetChild();
            child2.setName("child2");
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            return parent;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = loadParent(parentId);
            assertEquals(2, loadParent(parentId).getChildren().size());
            assertTrue(parent.getChildren().contains(parent.getChildren().iterator().next()));
            return null;
        }
    });
}
Also used : SetChild(com.vladmihalcea.hibernate.model.eagerset.SetChild) SetParent(com.vladmihalcea.hibernate.model.eagerset.SetParent) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 3 with SetParent

use of com.vladmihalcea.hibernate.model.eagerset.SetParent in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateEagerSetTest method testFixByPersistingChild.

@Test
public void testFixByPersistingChild() {
    final Long parentId = cleanAndSaveParent();
    SetParent parent = transactionTemplate.execute(new TransactionCallback<SetParent>() {

        @Override
        public SetParent doInTransaction(TransactionStatus transactionStatus) {
            SetParent parent = loadParent(parentId);
            SetChild child1 = new SetChild();
            child1.setName("child1");
            SetChild child2 = new SetChild();
            child2.setName("child2");
            entityManager.persist(child1);
            entityManager.persist(child2);
            parent.addChild(child1);
            parent.addChild(child2);
            entityManager.merge(parent);
            parent.getChildren().size();
            return parent;
        }
    });
    assertEquals(2, parent.getChildren().size());
}
Also used : SetChild(com.vladmihalcea.hibernate.model.eagerset.SetChild) SetParent(com.vladmihalcea.hibernate.model.eagerset.SetParent) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Aggregations

SetChild (com.vladmihalcea.hibernate.model.eagerset.SetChild)3 SetParent (com.vladmihalcea.hibernate.model.eagerset.SetParent)3 Test (org.junit.Test)3 TransactionStatus (org.springframework.transaction.TransactionStatus)3