Search in sources :

Example 11 with LazyInitializationException

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();
}
Also used : Transaction(org.hibernate.Transaction) LazyInitializationException(org.hibernate.LazyInitializationException) BigDecimal(java.math.BigDecimal) Session(org.hibernate.Session) Test(org.junit.Test)

Example 12 with LazyInitializationException

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();
}
Also used : LazyInitializationException(org.hibernate.LazyInitializationException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 13 with LazyInitializationException

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();
}
Also used : BeforeTransactionCompletionProcess(org.hibernate.action.spi.BeforeTransactionCompletionProcess) TransactionException(org.hibernate.TransactionException) LazyInitializationException(org.hibernate.LazyInitializationException) SessionImplementor(org.hibernate.engine.spi.SessionImplementor) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 14 with LazyInitializationException

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) {
    }
}
Also used : SessionStatistics(org.hibernate.stat.SessionStatistics) LazyInitializationException(org.hibernate.LazyInitializationException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 15 with LazyInitializationException

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();
}
Also used : RepositoryEntryLifecycle(org.olat.repository.model.RepositoryEntryLifecycle) LazyInitializationException(org.hibernate.LazyInitializationException) Date(java.util.Date) Test(org.junit.Test)

Aggregations

LazyInitializationException (org.hibernate.LazyInitializationException)15 Test (org.junit.Test)13 Session (org.hibernate.Session)5 TestForIssue (org.hibernate.testing.TestForIssue)4 TransactionStatus (org.springframework.transaction.TransactionStatus)3 Date (java.util.Date)2 List (java.util.List)2 SessionStatistics (org.hibernate.stat.SessionStatistics)2 TransactionCallback (org.springframework.transaction.support.TransactionCallback)2 BagBranch (com.vladmihalcea.hibernate.model.baglist.BagBranch)1 BagForest (com.vladmihalcea.hibernate.model.baglist.BagForest)1 BagLeaf (com.vladmihalcea.hibernate.model.baglist.BagLeaf)1 BagTree (com.vladmihalcea.hibernate.model.baglist.BagTree)1 Branch (com.vladmihalcea.hibernate.model.indexlist.Branch)1 Forest (com.vladmihalcea.hibernate.model.indexlist.Forest)1 Leaf (com.vladmihalcea.hibernate.model.indexlist.Leaf)1 Tree (com.vladmihalcea.hibernate.model.indexlist.Tree)1 Company (com.vladmihalcea.hibernate.model.store.Company)1 Image (com.vladmihalcea.hibernate.model.store.Image)1 Product (com.vladmihalcea.hibernate.model.store.Product)1