Search in sources :

Example 36 with HibernateException

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) {
    }
}
Also used : HibernateException(org.hibernate.HibernateException) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Test(org.junit.Test)

Example 37 with HibernateException

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

Example 38 with HibernateException

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

Example 39 with HibernateException

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();
}
Also used : Field(java.lang.reflect.Field) HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 40 with HibernateException

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

Aggregations

HibernateException (org.hibernate.HibernateException)609 Session (org.hibernate.Session)285 DAOException (com.tomasio.projects.trainning.exception.DAOException)186 DAOException (org.jbei.ice.storage.DAOException)122 ArrayList (java.util.ArrayList)63 Criteria (org.hibernate.Criteria)56 Test (org.junit.Test)43 Transaction (org.hibernate.Transaction)39 SQLException (java.sql.SQLException)38 SimpleDateFormat (java.text.SimpleDateFormat)22 Query (org.hibernate.Query)20 IOException (java.io.IOException)19 ParseException (java.text.ParseException)18 TestForIssue (org.hibernate.testing.TestForIssue)16 Date (java.util.Date)13 List (java.util.List)12 PersistenceException (org.mifos.framework.exceptions.PersistenceException)12 Serializable (java.io.Serializable)11 HashMap (java.util.HashMap)11 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)11