Search in sources :

Example 31 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateEqualsHashCodeTest method testRootObjects.

@Test
public void testRootObjects() {
    final Company newCompany = new Company();
    newCompany.setName("TV Company");
    final Long companyId = transactionTemplate.execute(new TransactionCallback<Long>() {

        @Override
        public Long doInTransaction(TransactionStatus transactionStatus) {
            entityManager.persist(newCompany);
            return newCompany.getId();
        }
    });
    Company detachedCompany = transactionTemplate.execute(new TransactionCallback<Company>() {

        @Override
        public Company doInTransaction(TransactionStatus transactionStatus) {
            Company attachedCompany = entityManager.find(Company.class, companyId);
            assertEquals(newCompany, attachedCompany);
            assertEquals(newCompany.hashCode(), attachedCompany.hashCode());
            return attachedCompany;
        }
    });
    assertEquals(newCompany, detachedCompany);
    assertEquals(newCompany.hashCode(), detachedCompany.hashCode());
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Company attachedCompany = entityManager.find(Company.class, companyId);
            attachedCompany.setName("New Company");
            entityManager.flush();
            return null;
        }
    });
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Company attachedCompany = entityManager.find(Company.class, companyId);
            assertEquals(newCompany, attachedCompany);
            assertEquals(newCompany.hashCode(), attachedCompany.hashCode());
            return null;
        }
    });
}
Also used : Company(com.vladmihalcea.hibernate.model.store.Company) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 32 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateEqualsHashCodeTest method testChildObjects.

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

        @Override
        public Long doInTransaction(TransactionStatus transactionStatus) {
            Company company = new Company();
            company.setName("TV Company");
            entityManager.persist(company);
            Product product = new Product("tvCode");
            product.setName("TV");
            product.setCompany(company);
            Image frontImage = new Image();
            frontImage.setName("front image");
            frontImage.setIndex(0);
            Image sideImage = new Image();
            sideImage.setName("side image");
            sideImage.setIndex(1);
            product.addImage(frontImage);
            product.addImage(sideImage);
            WarehouseProductInfo warehouseProductInfo = new WarehouseProductInfo();
            warehouseProductInfo.setQuantity(101);
            product.addWarehouse(warehouseProductInfo);
            entityManager.persist(product);
            return product.getId();
        }
    });
    Product product = transactionTemplate.execute(new TransactionCallback<Product>() {

        @Override
        public Product doInTransaction(TransactionStatus transactionStatus) {
            return entityManager.createQuery("select p " + "from Product p " + "left join fetch p.images i " + "where p.id = :productId", Product.class).setParameter("productId", productId).getSingleResult();
        }
    });
    Image frontImage = new Image();
    frontImage.setName("front image");
    frontImage.setProduct(product);
    frontImage.setIndex(0);
    assertTrue(product.getImages().contains(frontImage));
    List<Image> images = transactionTemplate.execute(new TransactionCallback<List<Image>>() {

        @Override
        public List<Image> doInTransaction(TransactionStatus transactionStatus) {
            return entityManager.createQuery("select i from Image i ", Image.class).getResultList();
        }
    });
    try {
        assertTrue(new HashSet<Image>(images).contains(frontImage));
        fail("Should have thrown LazyInitializationException!");
    } catch (LazyInitializationException expected) {
    }
}
Also used : WarehouseProductInfo(com.vladmihalcea.hibernate.model.store.WarehouseProductInfo) Company(com.vladmihalcea.hibernate.model.store.Company) TransactionStatus(org.springframework.transaction.TransactionStatus) Product(com.vladmihalcea.hibernate.model.store.Product) Image(com.vladmihalcea.hibernate.model.store.Image) LazyInitializationException(org.hibernate.LazyInitializationException) List(java.util.List) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 33 with TransactionStatus

use of org.springframework.transaction.TransactionStatus in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateJTATransactionIsolationTest method test.

@Test
public void test() {
    transactionTemplate.execute(new TransactionCallback<Void>() {

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Company company = new Company();
            company.setName("TV Company");
            entityManager.persist(company);
            Product product1 = new Product("tvCode");
            product1.setName("TV");
            product1.setCompany(company);
            Image frontImage1 = new Image();
            frontImage1.setName("front image 1");
            frontImage1.setIndex(0);
            Image sideImage1 = new Image();
            sideImage1.setName("side image 1");
            sideImage1.setIndex(1);
            product1.addImage(frontImage1);
            product1.addImage(sideImage1);
            WarehouseProductInfo warehouseProductInfo1 = new WarehouseProductInfo();
            warehouseProductInfo1.setQuantity(101);
            product1.addWarehouse(warehouseProductInfo1);
            entityManager.persist(product1);
            Product product = entityManager.find(Product.class, 1L);
            product.setQuantity(10);
            return null;
        }
    });
    storeService.purchase(1L);
}
Also used : WarehouseProductInfo(com.vladmihalcea.hibernate.model.store.WarehouseProductInfo) Company(com.vladmihalcea.hibernate.model.store.Company) TransactionStatus(org.springframework.transaction.TransactionStatus) Product(com.vladmihalcea.hibernate.model.store.Product) Image(com.vladmihalcea.hibernate.model.store.Image) Test(org.junit.Test)

Example 34 with TransactionStatus

use of org.springframework.transaction.TransactionStatus 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)

Example 35 with TransactionStatus

use of org.springframework.transaction.TransactionStatus 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

TransactionStatus (org.springframework.transaction.TransactionStatus)359 Test (org.junit.Test)186 TransactionCallbackWithoutResult (org.springframework.transaction.support.TransactionCallbackWithoutResult)154 TransactionCallback (org.springframework.transaction.support.TransactionCallback)91 TransactionTemplate (org.springframework.transaction.support.TransactionTemplate)72 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)32 List (java.util.List)28 ArrayList (java.util.ArrayList)27 SQLException (java.sql.SQLException)25 Connection (java.sql.Connection)21 Date (java.util.Date)18 PreparedStatement (java.sql.PreparedStatement)17 TransactionSynchronizationAdapter (org.springframework.transaction.support.TransactionSynchronizationAdapter)15 OnmsNode (org.opennms.netmgt.model.OnmsNode)14 ManagerException (com.alibaba.otter.manager.biz.common.exceptions.ManagerException)13 RepeatConfigureException (com.alibaba.otter.manager.biz.common.exceptions.RepeatConfigureException)13 UncategorizedSQLException (org.springframework.jdbc.UncategorizedSQLException)12 PlatformTransactionManager (org.springframework.transaction.PlatformTransactionManager)12 EntityManager (javax.persistence.EntityManager)11 DataSource (javax.sql.DataSource)11