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