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());
}
}
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());
});
}
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);
}
});
}
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));
}
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));
}
Aggregations