use of org.hibernate.HibernateException in project hibernate-orm by hibernate.
the class ExtendsTest method testMissingSuper.
@Test
public void testMissingSuper() {
try {
Metadata metadata = new MetadataSources(serviceRegistry).addResource(getBaseForMappings() + "extendshbm/Customer.hbm.xml").addResource(getBaseForMappings() + "extendshbm/Employee.hbm.xml").buildMetadata();
fail("Should not be able to build sessionFactory without a Person");
} catch (HibernateException e) {
}
}
use of org.hibernate.HibernateException in project hibernate-orm by hibernate.
the class ScrollableCollectionFetchingTest method testTupleReturnFails.
@Test
public void testTupleReturnFails() {
Session s = openSession();
Transaction txn = s.beginTransaction();
try {
s.createQuery("select a, a.weight from Animal a inner join fetch a.offspring").scroll();
fail("scroll allowed with collection fetch and reurning tuples");
} catch (IllegalArgumentException e) {
assertTyping(QueryException.class, e.getCause());
} catch (HibernateException e) {
// expected result...
}
txn.commit();
s.close();
}
use of org.hibernate.HibernateException in project hibernate-orm by hibernate.
the class WithClauseTest method testWithClauseFailsWithFetch.
@Test
public void testWithClauseFailsWithFetch() {
TestData data = new TestData();
data.prepare();
Session s = openSession();
Transaction txn = s.beginTransaction();
try {
s.createQuery("from Animal a inner join fetch a.offspring as o with o.bodyWeight = :someLimit").setDouble("someLimit", 1).list();
fail("ad-hoc on clause allowed with fetched association");
} catch (IllegalArgumentException e) {
assertTyping(QueryException.class, e.getCause());
} catch (HibernateException e) {
// the expected response...
}
txn.commit();
s.close();
data.cleanup();
}
use of org.hibernate.HibernateException in project hibernate-orm by hibernate.
the class MutableNaturalIdTest method testReattachmentUnmodifiedNaturalIdCheck.
@Test
public void testReattachmentUnmodifiedNaturalIdCheck() throws Throwable {
Session s = openSession();
s.beginTransaction();
User u = new User("gavin", "hb", "secret");
s.persist(u);
s.getTransaction().commit();
s.close();
s = openSession();
s.beginTransaction();
try {
s.buildLockRequest(LockOptions.NONE).lock(u);
Field name = u.getClass().getDeclaredField("name");
name.setAccessible(true);
name.set(u, "Gavin");
assertNotNull(s.byNaturalId(User.class).using("name", "Gavin").using("org", "hb").load());
s.getTransaction().commit();
} catch (HibernateException expected) {
s.getTransaction().rollback();
} catch (Throwable t) {
try {
s.getTransaction().rollback();
} catch (Throwable ignore) {
}
throw t;
} finally {
s.close();
}
s = openSession();
s.beginTransaction();
s.delete(u);
s.getTransaction().commit();
s.close();
}
use of org.hibernate.HibernateException in project hibernate-orm by hibernate.
the class SaveOrUpdateTest method testEvictThenSaveOrUpdate.
@Test
public void testEvictThenSaveOrUpdate() {
Session s = openSession();
s.getTransaction().begin();
Node parent = new Node("1:parent");
Node child = new Node("2:child");
Node grandchild = new Node("3:grandchild");
parent.addChild(child);
child.addChild(grandchild);
s.saveOrUpdate(parent);
s.getTransaction().commit();
s.close();
Session s1 = openSession();
s1.getTransaction().begin();
child = (Node) s1.load(Node.class, "2:child");
assertTrue(s1.contains(child));
assertFalse(Hibernate.isInitialized(child));
assertTrue(s1.contains(child.getParent()));
assertTrue(Hibernate.isInitialized(child));
assertFalse(Hibernate.isInitialized(child.getChildren()));
assertFalse(Hibernate.isInitialized(child.getParent()));
assertTrue(s1.contains(child));
s1.evict(child);
assertFalse(s1.contains(child));
assertTrue(s1.contains(child.getParent()));
Session s2 = openSession();
try {
s2.getTransaction().begin();
s2.saveOrUpdate(child);
fail();
} catch (HibernateException ex) {
// expected because parent is connected to s1
} finally {
s2.getTransaction().rollback();
}
s2.close();
s1.evict(child.getParent());
assertFalse(s1.contains(child.getParent()));
s2 = openSession();
s2.getTransaction().begin();
s2.saveOrUpdate(child);
assertTrue(s2.contains(child));
assertFalse(s1.contains(child));
assertTrue(s2.contains(child.getParent()));
assertFalse(s1.contains(child.getParent()));
assertFalse(Hibernate.isInitialized(child.getChildren()));
assertFalse(Hibernate.isInitialized(child.getParent()));
assertEquals(1, child.getChildren().size());
assertEquals("1:parent", child.getParent().getName());
assertTrue(Hibernate.isInitialized(child.getChildren()));
assertFalse(Hibernate.isInitialized(child.getParent()));
assertNull(child.getParent().getDescription());
assertTrue(Hibernate.isInitialized(child.getParent()));
s1.getTransaction().commit();
s2.getTransaction().commit();
s1.close();
s2.close();
s = openSession();
s.beginTransaction();
s.delete(s.get(Node.class, "3:grandchild"));
s.delete(s.get(Node.class, "2:child"));
s.delete(s.get(Node.class, "1:parent"));
s.getTransaction().commit();
s.close();
}
Aggregations