Search in sources :

Example 31 with HibernateException

use of org.hibernate.HibernateException in project hibernate-orm by hibernate.

the class ConfigurationObjectSettingTest method testContainerBootstrapValidationFactory.

@Test
public void testContainerBootstrapValidationFactory() {
    final Object token = new Object();
    PersistenceUnitInfoAdapter adapter = new PersistenceUnitInfoAdapter();
    try {
        Bootstrap.getEntityManagerFactoryBuilder(adapter, Collections.singletonMap(AvailableSettings.VALIDATION_FACTORY, token));
        fail("Was expecting error as token did not implement ValidatorFactory");
    } catch (HibernateException e) {
    // probably the condition we want but unfortunately the exception is not specific
    // and the pertinent info is in a cause
    }
}
Also used : HibernateException(org.hibernate.HibernateException) PersistenceUnitInfoAdapter(org.hibernate.jpa.test.PersistenceUnitInfoAdapter) Test(org.junit.Test)

Example 32 with HibernateException

use of org.hibernate.HibernateException in project hibernate-orm by hibernate.

the class MultipleSessionCollectionTest method testCopyInitializedCollectionReferenceToNewEntityCollectionRoleAfterGet.

@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testCopyInitializedCollectionReferenceToNewEntityCollectionRoleAfterGet() {
    Parent p = new Parent();
    Child c = new Child();
    p.children.add(c);
    Session s = openSession();
    s.getTransaction().begin();
    s.persist(p);
    s.getTransaction().commit();
    s.close();
    Session s1 = openSession();
    s1.getTransaction().begin();
    p = s1.get(Parent.class, p.id);
    Hibernate.initialize(p.children);
    // Copy p.children (initialized) into a different Parent.oldChildren (note different collection role)
    // and try to save in new session.
    Parent pWithSameChildren = new Parent();
    pWithSameChildren.oldChildren = p.children;
    Session s2 = openSession();
    s2.getTransaction().begin();
    try {
        s2.saveOrUpdate(pWithSameChildren);
        s2.getTransaction().commit();
        fail("should have thrown HibernateException");
    } catch (HibernateException ex) {
        log.error(ex);
        s2.getTransaction().rollback();
    } finally {
        s2.close();
    }
    // should still be able to commit in first session
    s1.getTransaction().commit();
    s1.close();
    s1 = openSession();
    s1.getTransaction().begin();
    Parent pGet = s1.get(Parent.class, p.id);
    assertEquals(c.id, pGet.children.iterator().next().id);
    session.delete(pGet);
    s1.getTransaction().commit();
    session.close();
}
Also used : HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 33 with HibernateException

use of org.hibernate.HibernateException in project hibernate-orm by hibernate.

the class MultipleSessionCollectionTest method testSaveOrUpdateOwnerWithInitializedCollectionInNewSession.

@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testSaveOrUpdateOwnerWithInitializedCollectionInNewSession() {
    Parent p = new Parent();
    Child c = new Child();
    p.children.add(c);
    Session s = openSession();
    s.getTransaction().begin();
    s.persist(p);
    s.getTransaction().commit();
    s.close();
    Session s1 = openSession();
    s1.getTransaction().begin();
    p = s1.get(Parent.class, p.id);
    Hibernate.initialize(p.children);
    // try to save the same entity (with an initialized collection) in a new session
    Session s2 = openSession();
    s2.getTransaction().begin();
    try {
        s2.saveOrUpdate(p);
        s2.getTransaction().commit();
        fail("should have thrown HibernateException");
    } catch (HibernateException ex) {
        log.error(ex);
        s2.getTransaction().rollback();
    } finally {
        s2.close();
    }
    // should still be able to commit in first session
    s1.getTransaction().commit();
    s1.close();
    s1 = openSession();
    s1.getTransaction().begin();
    Parent pGet = s1.get(Parent.class, p.id);
    assertEquals(c.id, pGet.children.iterator().next().id);
    session.delete(pGet);
    s1.getTransaction().commit();
    session.close();
}
Also used : HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 34 with HibernateException

use of org.hibernate.HibernateException in project hibernate-orm by hibernate.

the class MultipleSessionCollectionTest method testCopyUninitializedCollectionReferenceAfterGet.

@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testCopyUninitializedCollectionReferenceAfterGet() {
    Parent p = new Parent();
    Child c = new Child();
    p.children.add(c);
    Session s = openSession();
    s.getTransaction().begin();
    s.persist(p);
    s.getTransaction().commit();
    s.close();
    Session s1 = openSession();
    s1.getTransaction().begin();
    p = s1.get(Parent.class, p.id);
    assertFalse(Hibernate.isInitialized(p.children));
    // Copy p.children (uninitialized) into a different Parent and try to save in new session.
    Parent pWithSameChildren = new Parent();
    pWithSameChildren.children = p.children;
    Session s2 = openSession();
    s2.getTransaction().begin();
    try {
        s2.saveOrUpdate(pWithSameChildren);
        s2.getTransaction().commit();
        fail("should have thrown HibernateException");
    } catch (HibernateException ex) {
        log.error(ex);
        s2.getTransaction().rollback();
    } finally {
        s2.close();
    }
    // should still be able to commit in first session
    s1.getTransaction().commit();
    s1.close();
    s1 = openSession();
    s1.getTransaction().begin();
    Parent pGet = s1.get(Parent.class, p.id);
    assertEquals(c.id, pGet.children.iterator().next().id);
    session.delete(pGet);
    s1.getTransaction().commit();
    session.close();
}
Also used : HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 35 with HibernateException

use of org.hibernate.HibernateException in project hibernate-orm by hibernate.

the class ThreadLocalCurrentSessionTest method testTransactionProtection.

@Test
public void testTransactionProtection() {
    Session session = sessionFactory().getCurrentSession();
    try {
        session.createQuery("from Silly");
        fail("method other than beginTransaction() allowed");
    } catch (HibernateException e) {
    // ok
    }
}
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