use of com.vladmihalcea.hibernate.model.store.Company 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;
}
});
}
use of com.vladmihalcea.hibernate.model.store.Company 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.Company 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.Company in project vladmihalcea.wordpress.com by vladmihalcea.
the class HibernateLocalTxIntegrationTest method test.
@Test
@Transactional
public void test() {
Company company = new Company();
entityManager.persist(company);
entityManager.find(Company.class, company.getId());
entityManager.find(Company.class, company.getId());
}
use of com.vladmihalcea.hibernate.model.store.Company 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;
}
});
}
Aggregations