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();
}
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();
}
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();
}
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();
}
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
}
}
Aggregations