Search in sources :

Example 51 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ManyToManyAssociationClassGeneratedIdTest method testRemoveAndAddEqualElement.

@Test
public void testRemoveAndAddEqualElement() {
    deleteMembership(getUser(), getGroup(), getMembership());
    addMembership(getUser(), getGroup(), createMembership("membership"));
    Session s = openSession();
    s.beginTransaction();
    try {
        // The new membership is transient (it has a null surrogate ID), so
        // Hibernate assumes that it should be added to the collection.
        // Inserts are done beforeQuery deletes, so a ConstraintViolationException
        // will be thrown on the insert because the unique constraint on the
        // user and group IDs in the join table is violated. See HHH-2801.
        s.merge(getUser());
        s.getTransaction().commit();
        fail("should have failed because inserts are beforeQuery deletes");
    } catch (PersistenceException e) {
        s.getTransaction().rollback();
        // expected
        assertTyping(ConstraintViolationException.class, e.getCause());
    } finally {
        s.close();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Session(org.hibernate.Session) AbstractManyToManyAssociationClassTest(org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest) Test(org.junit.Test)

Example 52 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ManyToManyAssociationClassGeneratedIdTest method testRemoveAndAddEqualElementNonKeyModified.

@Test
public void testRemoveAndAddEqualElementNonKeyModified() {
    deleteMembership(getUser(), getGroup(), getMembership());
    Membership membershipNew = createMembership("membership");
    addMembership(getUser(), getGroup(), membershipNew);
    membershipNew.setName("membership1");
    Session s = openSession();
    s.beginTransaction();
    try {
        // The new membership is transient (it has a null surrogate ID), so
        // Hibernate assumes that it should be added to the collection.
        // Inserts are done beforeQuery deletes, so a ConstraintViolationException
        // will be thrown on the insert because the unique constraint on the
        // user and group IDs in the join table is violated. See HHH-2801.
        s.merge(getUser());
        s.getTransaction().commit();
        fail("should have failed because inserts are beforeQuery deletes");
    } catch (PersistenceException e) {
        s.getTransaction().rollback();
        // expected
        assertTyping(ConstraintViolationException.class, e.getCause());
    } finally {
        s.close();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) Membership(org.hibernate.test.manytomanyassociationclass.Membership) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) Session(org.hibernate.Session) AbstractManyToManyAssociationClassTest(org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest) Test(org.junit.Test)

Example 53 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ManyToManyAssociationClassGeneratedIdTest method testRemoveAndAddEqualCollection.

@Test
public void testRemoveAndAddEqualCollection() {
    deleteMembership(getUser(), getGroup(), getMembership());
    getUser().setMemberships(new HashSet());
    getGroup().setMemberships(new HashSet());
    addMembership(getUser(), getGroup(), createMembership("membership"));
    Session s = openSession();
    s.beginTransaction();
    try {
        // The new membership is transient (it has a null surrogate ID), so
        // Hibernate assumes that it should be added to the collection.
        // Inserts are done beforeQuery deletes, so a ConstraintViolationException
        // will be thrown on the insert because the unique constraint on the
        // user and group IDs in the join table is violated. See HHH-2801.
        s.merge(getUser());
        s.getTransaction().commit();
        fail("should have failed because inserts are beforeQuery deletes");
    } catch (PersistenceException e) {
        s.getTransaction().rollback();
        // expected
        assertTyping(ConstraintViolationException.class, e.getCause());
    } finally {
        s.close();
    }
}
Also used : PersistenceException(javax.persistence.PersistenceException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) HashSet(java.util.HashSet) Session(org.hibernate.Session) AbstractManyToManyAssociationClassTest(org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest) Test(org.junit.Test)

Example 54 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class ImmutableEntityNaturalIdTest method testNaturalIdCheck.

@Test
public void testNaturalIdCheck() throws Exception {
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Parent p = new Parent("alex");
    Child c = new Child("billy", p);
    s.persist(p);
    s.persist(c);
    t.commit();
    s.close();
    Field name = c.getClass().getDeclaredField("name");
    name.setAccessible(true);
    name.set(c, "phil");
    s = openSession();
    t = s.beginTransaction();
    try {
        s.saveOrUpdate(c);
        s.flush();
        fail("should have failed because immutable natural ID was altered");
    } catch (PersistenceException e) {
    // expected
    } finally {
        t.rollback();
        s.close();
    }
    name.set(c, "billy");
    s = openSession();
    t = s.beginTransaction();
    s.delete(c);
    s.delete(p);
    t.commit();
    s.close();
}
Also used : Field(java.lang.reflect.Field) Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) Session(org.hibernate.Session) Test(org.junit.Test)

Example 55 with PersistenceException

use of javax.persistence.PersistenceException in project hibernate-orm by hibernate.

the class MergeMultipleEntityCopiesAllowedTest method testNestedEntityNewerThanTopLevel.

@Test
public void testNestedEntityNewerThanTopLevel() {
    Item item = new Item();
    item.setName("item");
    Category category = new Category();
    category.setName("category");
    Session s = openSession();
    Transaction tx = s.beginTransaction();
    s.persist(item);
    s.persist(category);
    tx.commit();
    s.close();
    // Get category1_1 from a different session.
    s = openSession();
    Category category1_1 = (Category) s.get(Category.class, category.getId());
    s.close();
    // Get and update category1_2 to increment its version.
    s = openSession();
    tx = s.beginTransaction();
    Category category1_2 = (Category) s.get(Category.class, category.getId());
    category1_2.setName("new name");
    tx.commit();
    s.close();
    assertTrue(category1_2.getVersion() > category1_1.getVersion());
    category1_1.setExampleItem(item);
    item.setCategory(category1_2);
    s = openSession();
    tx = s.beginTransaction();
    try {
        // nested representation is newer than top lever representation.
        category1_1 = (Category) s.merge(category1_1);
        fail("should have failed because one representation is an older version.");
    } catch (PersistenceException e) {
        // expected
        assertTyping(StaleObjectStateException.class, e.getCause());
    } finally {
        tx.rollback();
        s.close();
    }
    cleanup();
}
Also used : Transaction(org.hibernate.Transaction) PersistenceException(javax.persistence.PersistenceException) StaleObjectStateException(org.hibernate.StaleObjectStateException) Session(org.hibernate.Session) Test(org.junit.Test)

Aggregations

PersistenceException (javax.persistence.PersistenceException)125 Test (org.junit.Test)66 Session (org.hibernate.Session)50 Transaction (org.hibernate.Transaction)29 EntityManager (javax.persistence.EntityManager)17 IOException (java.io.IOException)12 StaleObjectStateException (org.hibernate.StaleObjectStateException)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)9 SQLGrammarException (org.hibernate.exception.SQLGrammarException)8 TransactionRequiredException (javax.persistence.TransactionRequiredException)7 MessagesEvent (com.openmeap.event.MessagesEvent)5 EntityNotFoundException (javax.persistence.EntityNotFoundException)5 OptimisticLockException (javax.persistence.OptimisticLockException)5 TestForIssue (org.hibernate.testing.TestForIssue)5 GlobalSettings (com.openmeap.model.dto.GlobalSettings)4 NoResultException (javax.persistence.NoResultException)4 LockOptions (org.hibernate.LockOptions)4 StaleStateException (org.hibernate.StaleStateException)4