Search in sources :

Example 6 with Company

use of com.vladmihalcea.hibernate.model.store.Company in project vladmihalcea.wordpress.com by vladmihalcea.

the class CompanySchedulerTest method test.

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

        @Override
        public Void doInTransaction(TransactionStatus transactionStatus) {
            Company tvCompany = new Company();
            tvCompany.setName("TV Company");
            Company radioCompany = new Company();
            radioCompany.setName("Radio Company");
            entityManager.persist(tvCompany);
            entityManager.persist(radioCompany);
            return null;
        }
    });
    Thread.sleep(500);
}
Also used : Company(com.vladmihalcea.hibernate.model.store.Company) TransactionStatus(org.springframework.transaction.TransactionStatus) Test(org.junit.Test)

Example 7 with Company

use of com.vladmihalcea.hibernate.model.store.Company in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateSQLStatementCountTest 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);
            WarehouseProductInfo warehouseProductInfo1 = new WarehouseProductInfo();
            warehouseProductInfo1.setQuantity(101);
            product1.addWarehouse(warehouseProductInfo1);
            Product product2 = new Product("cdCode");
            product2.setName("CD");
            product2.setCompany(company);
            WarehouseProductInfo warehouseProductInfo2 = new WarehouseProductInfo();
            warehouseProductInfo2.setQuantity(50);
            product2.addWarehouse(warehouseProductInfo2);
            entityManager.persist(product1);
            entityManager.persist(product2);
            entityManager.flush();
            final JdbcTemplate otherDataSourceJdbcTemplate = new JdbcTemplate(otherDataSource);
            List<Map<String, Object>> versions = otherDataSourceJdbcTemplate.queryForList(" select * from version ");
            assertTrue(versions.isEmpty());
            return null;
        }
    });
    try {
        SQLStatementCountValidator.reset();
        warehouseProductInfoService.findAllWithNPlusOne();
        SQLStatementCountValidator.assertSelectCount(1);
    } catch (SQLSelectCountMismatchException e) {
        assertEquals(3, e.getRecorded());
    }
    SQLStatementCountValidator.reset();
    warehouseProductInfoService.findAllWithFetch();
    SQLStatementCountValidator.assertSelectCount(1);
    SQLStatementCountValidator.reset();
    warehouseProductInfoService.newWarehouseProductInfo();
    SQLStatementCountValidator.assertSelectCount(1);
    SQLStatementCountValidator.assertInsertCount(2);
}
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) List(java.util.List) SQLSelectCountMismatchException(com.vladmihalcea.sql.exception.SQLSelectCountMismatchException) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) Test(org.junit.Test)

Example 8 with Company

use of com.vladmihalcea.hibernate.model.store.Company in project vladmihalcea.wordpress.com by vladmihalcea.

the class WarehouseProductInfoServiceImpl method newWarehouseProductInfo.

@Override
@Transactional
public WarehouseProductInfo newWarehouseProductInfo() {
    LOGGER.info("newWarehouseProductInfo");
    Company company = entityManager.createQuery("from Company", Company.class).getResultList().get(0);
    Product product3 = new Product("phoneCode");
    product3.setName("Phone");
    product3.setCompany(company);
    WarehouseProductInfo warehouseProductInfo3 = new WarehouseProductInfo();
    warehouseProductInfo3.setQuantity(19);
    product3.addWarehouse(warehouseProductInfo3);
    entityManager.persist(product3);
    return warehouseProductInfo3;
}
Also used : WarehouseProductInfo(com.vladmihalcea.hibernate.model.store.WarehouseProductInfo) Company(com.vladmihalcea.hibernate.model.store.Company) Product(com.vladmihalcea.hibernate.model.store.Product) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with Company

use of com.vladmihalcea.hibernate.model.store.Company in project vladmihalcea.wordpress.com by vladmihalcea.

the class HibernateJPATransactionIsolationIntegrationTest 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)

Aggregations

Company (com.vladmihalcea.hibernate.model.store.Company)9 Test (org.junit.Test)8 TransactionStatus (org.springframework.transaction.TransactionStatus)7 Product (com.vladmihalcea.hibernate.model.store.Product)6 WarehouseProductInfo (com.vladmihalcea.hibernate.model.store.WarehouseProductInfo)6 Image (com.vladmihalcea.hibernate.model.store.Image)4 List (java.util.List)2 Transactional (org.springframework.transaction.annotation.Transactional)2 SQLSelectCountMismatchException (com.vladmihalcea.sql.exception.SQLSelectCountMismatchException)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 PersistenceException (javax.persistence.PersistenceException)1 LazyInitializationException (org.hibernate.LazyInitializationException)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)1