Search in sources :

Example 1 with Membership

use of org.hibernate.test.manytomanyassociationclass.Membership 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)

Aggregations

PersistenceException (javax.persistence.PersistenceException)1 Session (org.hibernate.Session)1 ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)1 AbstractManyToManyAssociationClassTest (org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest)1 Membership (org.hibernate.test.manytomanyassociationclass.Membership)1 Test (org.junit.Test)1