Search in sources :

Example 1 with LinkedParent

use of com.vladmihalcea.hibernate.model.linkedset.LinkedParent in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateLinkedSetTest method test.

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

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

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Set<LinkedChild> children = loadParent(parentId).getChildren();
            assertEquals(5, children.size());
            Iterator<LinkedChild> childIterator = children.iterator();
            assertEquals("child1", childIterator.next().getName());
            assertEquals("child2", childIterator.next().getName());
            assertEquals("child3", childIterator.next().getName());
            assertEquals("child4", childIterator.next().getName());
            assertEquals("child5", childIterator.next().getName());
            return null;
        }
    });
}
Also used : Set(java.util.Set) LinkedChild(com.vladmihalcea.hibernate.model.linkedset.LinkedChild) Iterator(java.util.Iterator) TransactionStatus(org.springframework.transaction.TransactionStatus) LinkedParent(com.vladmihalcea.hibernate.model.linkedset.LinkedParent) Test(org.junit.Test)

Aggregations

LinkedChild (com.vladmihalcea.hibernate.model.linkedset.LinkedChild)1 LinkedParent (com.vladmihalcea.hibernate.model.linkedset.LinkedParent)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 Test (org.junit.Test)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1