use of org.hibernate.LazyInitializationException in project hibernate-orm by hibernate.
the class ProxyTest method testProxyEviction.
@SuppressWarnings({ "unchecked" })
@Test
public void testProxyEviction() {
Session s = openSession();
Transaction t = s.beginTransaction();
Container container = new Container("container");
container.setOwner(new Owner("owner"));
container.setInfo(new Info("blah blah blah"));
container.getDataPoints().add(new DataPoint(new BigDecimal(1), new BigDecimal(1), "first data point"));
container.getDataPoints().add(new DataPoint(new BigDecimal(2), new BigDecimal(2), "second data point"));
s.save(container);
t.commit();
s.close();
s = openSession();
t = s.beginTransaction();
Container c = (Container) s.load(Container.class, container.getId());
assertFalse(Hibernate.isInitialized(c));
s.evict(c);
try {
c.getName();
fail("expecting LazyInitializationException");
} catch (LazyInitializationException e) {
// expected result
}
c = (Container) s.load(Container.class, container.getId());
assertFalse(Hibernate.isInitialized(c));
Info i = c.getInfo();
assertTrue(Hibernate.isInitialized(c));
assertFalse(Hibernate.isInitialized(i));
s.evict(c);
try {
i.getDetails();
fail("expecting LazyInitializationException");
} catch (LazyInitializationException e) {
// expected result
}
s.delete(c);
t.commit();
s.close();
}
use of org.hibernate.LazyInitializationException in project hibernate-orm by hibernate.
the class ListAddTest method addQuestionToDetachedQuizz.
@Test
public void addQuestionToDetachedQuizz() {
Session session = openSession();
session.beginTransaction();
Quizz quizz = session.get(Quizz.class, 1);
session.getTransaction().commit();
session.close();
assertFalse(((PersistentCollection) quizz.getQuestions()).wasInitialized());
try {
// this is the crux of the comment on HHH-9195 in regard to uninitialized, detached collections and
// not allowing additions
quizz.addQuestion(new Question(4, "question 4"));
// indexed-addition should fail
quizz.addQuestion(1, new Question(5, "question that should be at index 1"));
fail("Expecting a failure");
} catch (LazyInitializationException ignore) {
// expected
}
// session = openSession();
// session.beginTransaction();
// session.merge( quizz );
// session.getTransaction().commit();
// session.close();
//
// session = openSession();
// session.getTransaction().begin();
// quizz = session.get( Quizz.class, 1);
// assertEquals( 5, quizz.getQuestions().size() );
// assertEquals( 5, quizz.getQuestions().get( 1 ).getId().longValue() );
// session.getTransaction().commit();
// session.close();
}
use of org.hibernate.LazyInitializationException in project hibernate-orm by hibernate.
the class FooBarTest method testLazyCollectionsTouchedDuringPreCommit.
@Test
@TestForIssue(jiraKey = "HHH-7603")
public void testLazyCollectionsTouchedDuringPreCommit() 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();
// clear the session
s.clear();
// now reload the proxy and delete it
s.beginTransaction();
final Qux qToDelete = (Qux) s.load(Qux.class, q.getKey());
// register a pre commit process that will touch the collection and delete the entity
((EventSource) s).getActionQueue().registerProcess(new BeforeTransactionCompletionProcess() {
@Override
public void doBeforeTransactionCompletion(SessionImplementor session) {
qToDelete.getFums().size();
}
});
s.delete(qToDelete);
boolean ok = false;
try {
s.getTransaction().commit();
} catch (LazyInitializationException e) {
ok = true;
s.getTransaction().rollback();
} catch (TransactionException te) {
if (te.getCause() instanceof LazyInitializationException) {
ok = true;
}
s.getTransaction().rollback();
} finally {
s.close();
}
assertTrue("lazy collection should have blown in the before trans completion", ok);
s = openSession();
s.beginTransaction();
q = (Qux) s.load(Qux.class, q.getKey());
s.delete(q);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.LazyInitializationException in project hibernate-orm by hibernate.
the class PersistOnLazyCollectionTest method testPersistOnNewEntityRelatedToAlreadyPersistentEntityWithUninitializedLazyCollection.
@Test
@TestForIssue(jiraKey = "HHH-11916")
public void testPersistOnNewEntityRelatedToAlreadyPersistentEntityWithUninitializedLazyCollection() {
final Invoice _invoice = doInHibernate(this::sessionFactory, this::createInvoiceWithTwoInvoiceLines);
Invoice invoiceAfter = doInHibernate(this::sessionFactory, session -> {
SessionStatistics stats = session.getStatistics();
// load invoice, invoiceLines should not be loaded
Invoice invoice = session.get(Invoice.class, _invoice.getId());
Assert.assertEquals("Invoice lines should not be initialized while loading the invoice, " + "because of the lazy association.", 1, stats.getEntityCount());
Receipt receipt = new Receipt(RECEIPT_A);
receipt.setInvoice(invoice);
session.persist(receipt);
return invoice;
});
try {
invoiceAfter.getInvoiceLines().size();
fail("Should throw LazyInitializationException");
} catch (LazyInitializationException expected) {
}
}
use of org.hibernate.LazyInitializationException in project openolat by klemens.
the class RepositoryManagerTest method lazyLoadingCheck.
/**
* This is a simulation of OO-2667 to make sure that the LazyInitializationException don't
* set the transaction on rollback.
*/
@Test
public void lazyLoadingCheck() {
RepositoryEntry re = repositoryService.create("Rei Ayanami", "-", "Repository entry DAO Test 5", "", null);
dbInstance.commitAndCloseSession();
RepositoryEntryLifecycle cycle = lifecycleDao.create("New cycle 1", "New cycle soft 1", false, new Date(), new Date());
re = repositoryManager.setDescriptionAndName(re, "Updated repo entry", null, null, "", null, null, null, null, null, null, cycle);
dbInstance.commitAndCloseSession();
RepositoryEntry lazyRe = repositoryManager.setAccess(re, 2, false);
dbInstance.commitAndCloseSession();
try {
// produce the exception
lazyRe.getLifecycle().getValidFrom();
Assert.fail();
} catch (LazyInitializationException e) {
//
}
// load a fresh entry
RepositoryEntry entry = repositoryManager.lookupRepositoryEntry(lazyRe.getKey());
Date validFrom = entry.getLifecycle().getValidFrom();
Assert.assertNotNull(validFrom);
dbInstance.commitAndCloseSession();
}
Aggregations