use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class OptionalOneToOneMappedByTest method testBidirForeignIdGenerator.
// @OneToOne(mappedBy="address") with foreign generator
@Test
public void testBidirForeignIdGenerator() {
Session s = openSession();
Transaction tx = s.beginTransaction();
OwnerAddress address = new OwnerAddress();
address.setOwner(null);
try {
s.persist(address);
s.flush();
fail("should have failed with IdentifierGenerationException");
} catch (PersistenceException ex) {
assertTyping(IdentifierGenerationException.class, ex.getCause());
// expected
} finally {
tx.rollback();
}
s.close();
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class OracleFollowOnLockingTest method testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyDisablingFollowOnLockingThenFails.
@Test
public void testPessimisticLockWithMaxResultsAndOrderByWhileExplicitlyDisablingFollowOnLockingThenFails() {
final Session session = openSession();
session.beginTransaction();
sqlStatementInterceptor.getSqlQueries().clear();
try {
List<Product> products = session.createQuery("select p from Product p order by p.id", Product.class).setLockOptions(new LockOptions(LockMode.PESSIMISTIC_WRITE).setFollowOnLocking(false)).setMaxResults(10).getResultList();
fail("Should throw exception since Oracle does not support ORDER BY if follow on locking is disabled");
} catch (PersistenceException expected) {
assertEquals(SQLGrammarException.class, expected.getCause().getClass());
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToManyToOne.
@Test
public void testPersistOutsideTransactionCascadedToManyToOne() {
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
Session s = openSession();
try {
MyEntity myEntity = new MyEntity("test-persist");
myEntity.setSibling(new MySibling("test-persist-sibling-out"));
s.persist(myEntity);
assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
assertNull(myEntity.getId());
s.flush();
fail("TransactionRequiredException expected upon flush.");
} catch (PersistenceException ex) {
// expected
assertTyping(TransactionRequiredException.class, ex);
} finally {
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class IdentityGeneratedKeysTest method testPersistOutsideTransactionCascadedToInverseCollection.
@Test
@SuppressWarnings({ "unchecked" })
public void testPersistOutsideTransactionCascadedToInverseCollection() {
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
Session s = openSession();
try {
MyEntity myEntity2 = new MyEntity("test-persist-2");
MyChild child = new MyChild("test-child-persist-inverse");
myEntity2.getInverseChildren().add(child);
child.setInverseParent(myEntity2);
s.persist(myEntity2);
assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
assertNull(myEntity2.getId());
s.flush();
fail("TransactionRequiredException expected upon flush.");
} catch (PersistenceException ex) {
// expected
assertTyping(TransactionRequiredException.class, ex);
} finally {
s.close();
}
}
use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.
the class IdentityGeneratedKeysTest method testPersistOutsideTransaction.
@Test
public void testPersistOutsideTransaction() {
Session s = openSession();
try {
// first test save() which should force an immediate insert...
MyEntity myEntity1 = new MyEntity("test-save");
Long id = (Long) s.save(myEntity1);
assertNotNull("identity column did not force immediate insert", id);
assertEquals(id, myEntity1.getId());
// next test persist() which should cause a delayed insert...
long initialInsertCount = sessionFactory().getStatistics().getEntityInsertCount();
MyEntity myEntity2 = new MyEntity("test-persist");
s.persist(myEntity2);
assertEquals("persist on identity column not delayed", initialInsertCount, sessionFactory().getStatistics().getEntityInsertCount());
assertNull(myEntity2.getId());
// an explicit flush should cause execution of the delayed insertion
s.flush();
fail("TransactionRequiredException required upon flush");
} catch (PersistenceException ex) {
// expected
assertTyping(TransactionRequiredException.class, ex);
} finally {
s.close();
}
}
Aggregations