use of org.hibernate.LazyInitializationException in project vladmihalcea.wordpress.com by vladmihalcea.
the class HibernateBagMultiLevelFetchTest method test.
@Test
public void test() {
final Long forestId = transactionTemplate.execute(new TransactionCallback<Long>() {
@Override
public Long doInTransaction(TransactionStatus transactionStatus) {
BagForest forest = new BagForest();
BagTree tree1 = new BagTree();
tree1.setIndex(0);
BagBranch branch11 = new BagBranch();
branch11.setIndex(0);
BagLeaf leaf111 = new BagLeaf();
leaf111.setIndex(0);
BagLeaf leaf112 = new BagLeaf();
leaf111.setIndex(1);
BagLeaf leaf113 = new BagLeaf();
leaf111.setIndex(2);
BagLeaf leaf114 = new BagLeaf();
leaf111.setIndex(3);
branch11.addLeaf(leaf111);
branch11.addLeaf(leaf112);
branch11.addLeaf(leaf113);
branch11.addLeaf(leaf114);
BagBranch branch12 = new BagBranch();
branch12.setIndex(1);
BagLeaf leaf121 = new BagLeaf();
leaf121.setIndex(1);
BagLeaf leaf122 = new BagLeaf();
leaf122.setIndex(2);
BagLeaf leaf123 = new BagLeaf();
leaf123.setIndex(3);
BagLeaf leaf124 = new BagLeaf();
leaf124.setIndex(4);
branch12.addLeaf(leaf121);
branch12.addLeaf(leaf122);
branch12.addLeaf(leaf123);
branch12.addLeaf(leaf124);
tree1.addBranch(branch11);
tree1.addBranch(branch12);
BagTree tree2 = new BagTree();
tree2.setIndex(1);
BagBranch branch21 = new BagBranch();
branch21.setIndex(0);
BagLeaf leaf211 = new BagLeaf();
leaf211.setIndex(0);
BagLeaf leaf212 = new BagLeaf();
leaf111.setIndex(1);
BagLeaf leaf213 = new BagLeaf();
leaf111.setIndex(2);
BagLeaf leaf214 = new BagLeaf();
leaf111.setIndex(3);
branch21.addLeaf(leaf211);
branch21.addLeaf(leaf212);
branch21.addLeaf(leaf213);
branch21.addLeaf(leaf214);
BagBranch branch22 = new BagBranch();
branch22.setIndex(2);
BagLeaf leaf221 = new BagLeaf();
leaf121.setIndex(0);
BagLeaf leaf222 = new BagLeaf();
leaf121.setIndex(1);
BagLeaf leaf223 = new BagLeaf();
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();
}
});
BagForest forest = transactionTemplate.execute(new TransactionCallback<BagForest>() {
@Override
public BagForest doInTransaction(TransactionStatus transactionStatus) {
return entityManager.find(BagForest.class, forestId);
}
});
try {
navigateForest(forest);
fail("Should have thrown LazyInitializationException!");
} catch (LazyInitializationException expected) {
}
forest = transactionTemplate.execute(new TransactionCallback<BagForest>() {
@Override
public BagForest doInTransaction(TransactionStatus transactionStatus) {
BagForest forest = entityManager.find(BagForest.class, forestId);
navigateForest(forest);
return forest;
}
});
try {
forest = transactionTemplate.execute(new TransactionCallback<BagForest>() {
@Override
public BagForest doInTransaction(TransactionStatus transactionStatus) {
BagForest forest = entityManager.createQuery("select f " + "from BagForest f " + "join fetch f.trees t " + "join fetch t.branches b " + "join fetch b.leaves l ", BagForest.class).getSingleResult();
return forest;
}
});
fail("Should have thrown MultipleBagFetchException!");
} catch (PersistenceException expected) {
assertEquals(MultipleBagFetchException.class, expected.getCause().getClass());
}
List<BagLeaf> leaves = transactionTemplate.execute(new TransactionCallback<List<BagLeaf>>() {
@Override
public List<BagLeaf> doInTransaction(TransactionStatus transactionStatus) {
List<BagLeaf> leaves = entityManager.createQuery("select l " + "from BagLeaf l " + "inner join fetch l.branch b " + "inner join fetch b.tree t " + "inner join fetch t.forest f " + "where f.id = :forestId", BagLeaf.class).setParameter("forestId", forestId).getResultList();
return leaves;
}
});
forest = reconstructForest(leaves, forestId);
navigateForest(forest);
final BagBranch firstBranch = forest.getTrees().get(0).getBranches().get(0);
firstBranch.getLeaves().clear();
final BagForest toMergeForest = forest;
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
BagForest savedForest = entityManager.merge(toMergeForest);
if (!firstBranch.getLeaves().equals(savedForest.getTrees().get(0).getBranches().get(0).getLeaves())) {
LOG.error("Unsafe reusing the bag, changes haven't propagated!");
}
entityManager.flush();
return null;
}
});
transactionTemplate.execute(new TransactionCallback<Void>() {
@Override
public Void doInTransaction(TransactionStatus status) {
BagForest savedForest = entityManager.find(BagForest.class, forestId);
if (!firstBranch.getLeaves().equals(savedForest.getTrees().get(0).getBranches().get(0).getLeaves())) {
LOG.error("Unsafe reusing the bag, changes haven't propagated!");
}
return null;
}
});
}
use of org.hibernate.LazyInitializationException 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 org.hibernate.LazyInitializationException 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);
}
use of org.hibernate.LazyInitializationException in project hibernate-orm by hibernate.
the class LazyLoadingNotFoundTest method testNonExistentLazyInitOutsideTransaction.
@Test
@TestForIssue(jiraKey = "HHH-11179")
public void testNonExistentLazyInitOutsideTransaction() {
Session s = openSession();
s.beginTransaction();
Child loadedChild = s.load(Child.class, -1L);
s.getTransaction().commit();
s.close();
try {
loadedChild.getParent();
fail("lazy init did not fail on non-existent proxy");
} catch (LazyInitializationException e) {
assertNotNull(e.getMessage());
}
}
use of org.hibernate.LazyInitializationException in project hibernate-orm by hibernate.
the class FooBarTest method testLazyCollections.
@Test
public void testLazyCollections() throws Exception {
Session s = openSession();
s.beginTransaction();
Qux q = new Qux();
s.save(q);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
q = (Qux) s.load(Qux.class, q.getKey());
s.getTransaction().commit();
s.close();
System.out.println("Two exceptions are supposed to occur:");
boolean ok = false;
try {
q.getMoreFums().isEmpty();
} catch (LazyInitializationException e) {
ok = true;
}
assertTrue("lazy collection with one-to-many", ok);
ok = false;
try {
q.getFums().isEmpty();
} catch (LazyInitializationException e) {
ok = true;
}
assertTrue("lazy collection with many-to-many", ok);
s = openSession();
s.beginTransaction();
q = (Qux) s.load(Qux.class, q.getKey());
s.delete(q);
s.getTransaction().commit();
s.close();
}
Aggregations