use of com.vladmihalcea.hibernate.model.store.WarehouseProductInfo 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) {
}
}
use of com.vladmihalcea.hibernate.model.store.WarehouseProductInfo 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);
}
use of com.vladmihalcea.hibernate.model.store.WarehouseProductInfo in project vladmihalcea.wordpress.com by vladmihalcea.
the class HibernateOperationsOrderTest method test.
@Test
public void test() {
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();
}
});
try {
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus transactionStatus) {
Product product = entityManager.find(Product.class, productId);
assertEquals(2, product.getImages().size());
Iterator<Image> imageIterator = product.getImages().iterator();
Image frontImage = imageIterator.next();
assertEquals("front image", frontImage.getName());
assertEquals(0, frontImage.getIndex());
Image sideImage = imageIterator.next();
assertEquals("side image", sideImage.getName());
assertEquals(1, sideImage.getIndex());
Image backImage = new Image();
backImage.setName("back image");
backImage.setIndex(1);
product.removeImage(sideImage);
product.addImage(backImage);
product.setName("tv set");
entityManager.flush();
return null;
}
});
fail("Expected ConstraintViolationException");
} catch (PersistenceException expected) {
assertEquals(ConstraintViolationException.class, expected.getCause().getClass());
}
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus transactionStatus) {
Product product = entityManager.find(Product.class, productId);
assertEquals(2, product.getImages().size());
Iterator<Image> imageIterator = product.getImages().iterator();
Image frontImage = imageIterator.next();
assertEquals("front image", frontImage.getName());
Image sideImage = imageIterator.next();
assertEquals("side image", sideImage.getName());
Image backImage = new Image();
backImage.setName("back image");
backImage.setIndex(1);
//http://docs.jboss.org/hibernate/orm/4.2/javadocs/org/hibernate/event/internal/AbstractFlushingEventListener.html#performExecutions%28org.hibernate.event.spi.EventSource%29
product.removeImage(sideImage);
entityManager.flush();
product.addImage(backImage);
product.setName("tv set");
entityManager.flush();
return null;
}
});
}
use of com.vladmihalcea.hibernate.model.store.WarehouseProductInfo 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);
}
use of com.vladmihalcea.hibernate.model.store.WarehouseProductInfo 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;
}
Aggregations