Search in sources :

Example 1 with Item

use of org.hibernate.orm.test.jpa.model.Item in project hibernate-orm by hibernate.

the class LockExceptionTests method testLockTimeoutFind.

@Test
@TestForIssue(jiraKey = "HHH-8786")
public void testLockTimeoutFind() {
    final Item item = new Item("find");
    inTransaction(session -> session.persist(item));
    try {
        inTransaction(session -> {
            session.find(Item.class, item.getId(), LockModeType.PESSIMISTIC_WRITE);
            TransactionUtil2.inTransaction(sessionFactory(), secondSession -> {
                try {
                    secondSession.find(Item.class, item.getId(), LockModeType.PESSIMISTIC_WRITE, Collections.singletonMap(AvailableSettings.JAKARTA_LOCK_TIMEOUT, LockOptions.NO_WAIT));
                    fail("Expecting a failure");
                } catch (LockTimeoutException | PessimisticLockException expected) {
                // expected outcome
                }
            });
        });
    } finally {
        inTransaction(session -> session.createQuery("delete Item").executeUpdate());
    }
}
Also used : Item(org.hibernate.orm.test.jpa.model.Item) LockTimeoutException(jakarta.persistence.LockTimeoutException) PessimisticLockException(jakarta.persistence.PessimisticLockException) AbstractJPATest(org.hibernate.orm.test.jpa.model.AbstractJPATest) Test(org.junit.jupiter.api.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 2 with Item

use of org.hibernate.orm.test.jpa.model.Item in project hibernate-orm by hibernate.

the class RepeatableReadTest method testStaleNonVersionedInstanceFoundInQueryResult.

@Test
public void testStaleNonVersionedInstanceFoundInQueryResult() {
    String check = "Lock Modes";
    Part p = new Part(new Item("EJB3 Specification"), check, "3.3.5.3", new BigDecimal(0.0));
    inTransaction(session -> {
        session.save(p);
    });
    Long partId = p.getId();
    // Now, open a new Session and re-load the part...
    inTransaction(s1 -> {
        Part part = s1.get(Part.class, partId);
        // now that the item is associated with the persistence-context of that session,
        // open a new session and modify it "behind the back" of the first session
        inTransaction(s2 -> {
            Part part2 = s2.get(Part.class, partId);
            part2.setName("Lock Mode Types");
        });
        // at this point, s1 now contains stale data, so try an hql query which
        // returns said part and make sure we get the previously associated state
        // (i.e., the old name)
        Part part2 = (Part) s1.createQuery("select p from Part p").list().get(0);
        assertTrue(part == part2);
        assertEquals(check, part2.getName(), "encountered non-repeatable read");
    });
    // clean up
    inTransaction(session -> {
        Part part = (Part) session.createQuery("select p from Part p").list().get(0);
        session.delete(part);
        session.delete(part.getItem());
    });
}
Also used : Item(org.hibernate.orm.test.jpa.model.Item) Part(org.hibernate.orm.test.jpa.model.Part) BigDecimal(java.math.BigDecimal) AbstractJPATest(org.hibernate.orm.test.jpa.model.AbstractJPATest) Test(org.junit.jupiter.api.Test)

Example 3 with Item

use of org.hibernate.orm.test.jpa.model.Item in project hibernate-orm by hibernate.

the class JPAProxyTest method testGetSemantics.

/**
 * The ejb3 find() method maps to the Hibernate get() method
 */
@Test
public void testGetSemantics() {
    Long nonExistentId = new Long(-1);
    inTransaction(session -> {
        Item item = session.get(Item.class, nonExistentId);
        assertNull(item, "get() of non-existent entity did not return null");
    });
    inTransaction(s -> {
        // first load() it to generate a proxy...
        Item item = s.load(Item.class, nonExistentId);
        assertFalse(Hibernate.isInitialized(item));
        // then try to get() it to make sure we get an exception
        try {
            s.get(Item.class, nonExistentId);
            fail("force load did not fail on non-existent entity");
        } catch (EntityNotFoundException e) {
        // expected behavior
        } catch (AssertionFailedError e) {
            throw e;
        } catch (Throwable t) {
            fail("unexpected exception type on non-existent entity force load : " + t);
        }
    });
}
Also used : Item(org.hibernate.orm.test.jpa.model.Item) EntityNotFoundException(jakarta.persistence.EntityNotFoundException) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.jupiter.api.Test) AbstractJPATest(org.hibernate.orm.test.jpa.model.AbstractJPATest)

Example 4 with Item

use of org.hibernate.orm.test.jpa.model.Item in project hibernate-orm by hibernate.

the class RemovedEntityTest method testRemoveChildThenFlushWithCascadePersist.

@Test
public void testRemoveChildThenFlushWithCascadePersist() {
    Item item = new Item();
    inTransaction(session -> {
        item.setName("dummy");
        Part child = new Part(item, "child", "1234", BigDecimal.ONE);
        // persist cascades to part
        session.persist(item);
        // delete the part
        session.delete(child);
        assertFalse(session.contains(child), "the child is contained in the session, since it is deleted");
        // now try to flush, which will attempt to cascade persist again to child.
        session.flush();
        assertTrue(session.contains(child), "Now it is consistent again since if was cascade-persisted by the flush()");
    });
    // clean up
    inTransaction(session -> session.delete(item));
}
Also used : Item(org.hibernate.orm.test.jpa.model.Item) Part(org.hibernate.orm.test.jpa.model.Part) Test(org.junit.jupiter.api.Test) AbstractJPATest(org.hibernate.orm.test.jpa.model.AbstractJPATest)

Example 5 with Item

use of org.hibernate.orm.test.jpa.model.Item in project hibernate-orm by hibernate.

the class RemovedEntityTest method testRemoveThenSaveWithCascades.

@Test
public void testRemoveThenSaveWithCascades() {
    Item item = new Item();
    inTransaction(session -> {
        item.setName("dummy");
        Part part = new Part(item, "child", "1234", BigDecimal.ONE);
        // persist cascades to part
        session.persist(item);
        // delete cascades to part also
        session.delete(item);
        assertFalse(session.contains(item), "the item is contained in the session after deletion");
        assertFalse(session.contains(part), "the part is contained in the session after deletion");
        // now try to persist again as a "unschedule removal" operation
        session.persist(item);
        assertTrue(session.contains(item), "the item is contained in the session after deletion");
        assertTrue(session.contains(part), "the part is contained in the session after deletion");
    });
    // clean up
    inTransaction(session -> session.delete(item));
}
Also used : Item(org.hibernate.orm.test.jpa.model.Item) Part(org.hibernate.orm.test.jpa.model.Part) Test(org.junit.jupiter.api.Test) AbstractJPATest(org.hibernate.orm.test.jpa.model.AbstractJPATest)

Aggregations

AbstractJPATest (org.hibernate.orm.test.jpa.model.AbstractJPATest)17 Item (org.hibernate.orm.test.jpa.model.Item)17 Test (org.junit.jupiter.api.Test)17 Part (org.hibernate.orm.test.jpa.model.Part)4 LockTimeoutException (jakarta.persistence.LockTimeoutException)3 PessimisticLockException (jakarta.persistence.PessimisticLockException)3 EntityNotFoundException (jakarta.persistence.EntityNotFoundException)2 BigDecimal (java.math.BigDecimal)2 Session (org.hibernate.Session)2 TestForIssue (org.hibernate.testing.TestForIssue)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 AssertionFailedError (junit.framework.AssertionFailedError)1 StaleObjectStateException (org.hibernate.StaleObjectStateException)1 Transaction (org.hibernate.Transaction)1 SQLServerDialect (org.hibernate.dialect.SQLServerDialect)1 SQLGrammarException (org.hibernate.exception.SQLGrammarException)1 MyEntity (org.hibernate.orm.test.jpa.model.MyEntity)1 Query (org.hibernate.query.Query)1 SemanticException (org.hibernate.query.SemanticException)1