Search in sources :

Example 91 with HibernateException

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

the class MultipleSessionCollectionTest method testDeleteCopyToNewOwnerNewCollectionRoleInNewSessionBeforeFlush.

@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testDeleteCopyToNewOwnerNewCollectionRoleInNewSessionBeforeFlush() {
    Parent p1 = new Parent();
    p1.nickNames.add("nick");
    Parent p2 = new Parent();
    Session s1 = openSession();
    s1.getTransaction().begin();
    s1.save(p1);
    s1.save(p2);
    s1.getTransaction().commit();
    s1.close();
    s1 = openSession();
    s1.getTransaction().begin();
    s1.delete(p1);
    // Assign the deleted collection to a different entity with different collection role (p2.oldNickNames)
    // beforeQuery committing delete.
    p2.oldNickNames = p1.nickNames;
    Session s2 = openSession();
    s2.getTransaction().begin();
    try {
        s2.saveOrUpdate(p2);
        fail("should have thrown HibernateException");
    } catch (HibernateException ex) {
        log.error(ex);
        s2.getTransaction().rollback();
    } finally {
        s2.close();
    }
    // should still be able to commit the original delete
    s1.getTransaction().commit();
    s1.close();
}
Also used : HibernateException(org.hibernate.HibernateException) Session(org.hibernate.Session) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 92 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 93 with HibernateException

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

the class MultipleSessionCollectionTest method testCopyPersistentCollectionReferenceBeforeFlush.

//
@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testCopyPersistentCollectionReferenceBeforeFlush() {
    Parent p = new Parent();
    Child c = new Child();
    p.children.add(c);
    Session s1 = openSession();
    s1.getTransaction().begin();
    s1.persist(p);
    // Copy p.children into a different Parent beforeQuery flush 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 94 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 95 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)366 DAOException (org.jbei.ice.storage.DAOException)141 Session (org.hibernate.Session)71 Test (org.junit.Test)41 ArrayList (java.util.ArrayList)30 SQLException (java.sql.SQLException)27 TestForIssue (org.hibernate.testing.TestForIssue)15 IOException (java.io.IOException)14 Transaction (org.hibernate.Transaction)14 Group (org.jbei.ice.storage.model.Group)14 Type (org.hibernate.type.Type)12 PersistenceException (org.mifos.framework.exceptions.PersistenceException)12 Serializable (java.io.Serializable)11 EntityEntry (org.hibernate.engine.spi.EntityEntry)10 SessionFactoryImplementor (org.hibernate.engine.spi.SessionFactoryImplementor)10 Method (java.lang.reflect.Method)8 HashMap (java.util.HashMap)8 Dialect (org.hibernate.dialect.Dialect)8 CollectionPersister (org.hibernate.persister.collection.CollectionPersister)8 HibernateProxy (org.hibernate.proxy.HibernateProxy)8