Search in sources :

Example 1 with Forest

use of com.vladmihalcea.hibernate.model.indexlist.Forest in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateListMultiLevelFetchTest method test.

@Test
public void test() {
    final Long forestId = transactionTemplate.execute(new TransactionCallback<Long>() {

        @Override
        public Long doInTransaction(TransactionStatus transactionStatus) {
            Forest forest = new Forest();
            Tree tree1 = new Tree();
            tree1.setIndex(0);
            Branch branch11 = new Branch();
            branch11.setIndex(0);
            Leaf leaf111 = new Leaf();
            leaf111.setIndex(0);
            Leaf leaf112 = new Leaf();
            leaf111.setIndex(1);
            Leaf leaf113 = new Leaf();
            leaf111.setIndex(2);
            Leaf leaf114 = new Leaf();
            leaf111.setIndex(3);
            branch11.addLeaf(leaf111);
            branch11.addLeaf(leaf112);
            branch11.addLeaf(leaf113);
            branch11.addLeaf(leaf114);
            Branch branch12 = new Branch();
            branch12.setIndex(1);
            Leaf leaf121 = new Leaf();
            leaf121.setIndex(1);
            Leaf leaf122 = new Leaf();
            leaf122.setIndex(2);
            Leaf leaf123 = new Leaf();
            leaf123.setIndex(3);
            Leaf leaf124 = new Leaf();
            leaf124.setIndex(4);
            branch12.addLeaf(leaf121);
            branch12.addLeaf(leaf122);
            branch12.addLeaf(leaf123);
            branch12.addLeaf(leaf124);
            tree1.addBranch(branch11);
            tree1.addBranch(branch12);
            Tree tree2 = new Tree();
            tree2.setIndex(1);
            Branch branch21 = new Branch();
            branch21.setIndex(0);
            Leaf leaf211 = new Leaf();
            leaf211.setIndex(0);
            Leaf leaf212 = new Leaf();
            leaf111.setIndex(1);
            Leaf leaf213 = new Leaf();
            leaf111.setIndex(2);
            Leaf leaf214 = new Leaf();
            leaf111.setIndex(3);
            branch21.addLeaf(leaf211);
            branch21.addLeaf(leaf212);
            branch21.addLeaf(leaf213);
            branch21.addLeaf(leaf214);
            Branch branch22 = new Branch();
            branch22.setIndex(2);
            Leaf leaf221 = new Leaf();
            leaf121.setIndex(0);
            Leaf leaf222 = new Leaf();
            leaf121.setIndex(1);
            Leaf leaf223 = new Leaf();
            leaf121.setIndex(2);
            branch22.addLeaf(leaf221);
            branch22.addLeaf(leaf222);
            branch22.addLeaf(leaf223);
            tree2.addBranch(branch21);
            tree2.addBranch(branch22);
            forest.addTree(tree1);
            forest.addTree(tree2);
            entityManager.persist(forest);
            entityManager.flush();
            return forest.getId();
        }
    });
    Forest forest = transactionTemplate.execute(new TransactionCallback<Forest>() {

        @Override
        public Forest doInTransaction(TransactionStatus transactionStatus) {
            return entityManager.find(Forest.class, forestId);
        }
    });
    try {
        navigateForest(forest);
        fail("Should have thrown LazyInitializationException!");
    } catch (LazyInitializationException expected) {
    }
    forest = transactionTemplate.execute(new TransactionCallback<Forest>() {

        @Override
        public Forest doInTransaction(TransactionStatus transactionStatus) {
            Forest f = entityManager.createQuery("select f " + "from Forest f " + "join fetch f.trees t " + "join fetch t.branches b " + "join fetch b.leaves l ", Forest.class).getSingleResult();
            return f;
        }
    });
    navigateForest(forest);
}
Also used : TransactionCallback(org.springframework.transaction.support.TransactionCallback) Branch(com.vladmihalcea.hibernate.model.indexlist.Branch) LazyInitializationException(org.hibernate.LazyInitializationException) TransactionStatus(org.springframework.transaction.TransactionStatus) Forest(com.vladmihalcea.hibernate.model.indexlist.Forest) Tree(com.vladmihalcea.hibernate.model.indexlist.Tree) Leaf(com.vladmihalcea.hibernate.model.indexlist.Leaf) Test(org.junit.Test)

Aggregations

Branch (com.vladmihalcea.hibernate.model.indexlist.Branch)1 Forest (com.vladmihalcea.hibernate.model.indexlist.Forest)1 Leaf (com.vladmihalcea.hibernate.model.indexlist.Leaf)1 Tree (com.vladmihalcea.hibernate.model.indexlist.Tree)1 LazyInitializationException (org.hibernate.LazyInitializationException)1 Test (org.junit.Test)1 TransactionStatus (org.springframework.transaction.TransactionStatus)1 TransactionCallback (org.springframework.transaction.support.TransactionCallback)1