Search in sources :

Example 6 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project hibernate-orm by hibernate.

the class HiloOptimizerConcurrencyTest method testTwoSessionsSerialGeneration.

@Test
public void testTwoSessionsSerialGeneration() {
    createSchema = true;
    rebuildSessionFactory();
    StandardServiceRegistry serviceRegistry = serviceRegistry();
    SessionFactoryImplementor sessionFactory = sessionFactory();
    try {
        final Session session1 = openSession();
        try {
            session1.beginTransaction();
            HibPerson p = new HibPerson();
            session1.save(p);
        } finally {
            session1.getTransaction().commit();
        }
        createSchema = false;
        buildResources();
        final Session session2 = openSession();
        session2.beginTransaction();
        try {
            HibPerson p = new HibPerson();
            session2.save(p);
        } finally {
            session2.getTransaction().commit();
        }
        for (int i = 2; i < 6; i++) {
            session1.beginTransaction();
            try {
                HibPerson p = new HibPerson();
                session1.save(p);
            } finally {
                session1.getTransaction().commit();
            }
        }
        session2.beginTransaction();
        try {
            HibPerson p = new HibPerson();
            session2.save(p);
        } finally {
            session2.getTransaction().commit();
        }
        session1.beginTransaction();
        try {
            HibPerson p = new HibPerson();
            session1.save(p);
        } finally {
            session1.getTransaction().commit();
        }
    } catch (ConstraintViolationException cve) {
        fail("ConstraintViolationException: " + cve.getMessage());
    } finally {
        releaseResources(serviceRegistry, sessionFactory);
    }
}
Also used : SessionFactoryImplementor(org.hibernate.engine.spi.SessionFactoryImplementor) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) Test(org.junit.Test)

Example 7 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException 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 before 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 before 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 8 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException 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 before 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 before 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 9 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException 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 before 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 before 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 10 with ConstraintViolationException

use of org.hibernate.exception.ConstraintViolationException in project herd by FINRAOS.

the class HerdErrorInformationExceptionHandler method isCausedByConstraintViolationException.

/**
 * Returns {@code true} if the given throwable is or is not caused by a database constraint violation.
 *
 * @param exception - throwable to check.
 *
 * @return {@code true} if is constraint violation, {@code false} otherwise.
 */
private boolean isCausedByConstraintViolationException(Exception exception) {
    // some databases will throw ConstraintViolationException
    boolean isConstraintViolation = ExceptionUtils.indexOfThrowable(exception, ConstraintViolationException.class) != -1;
    // other databases will not throw a nice exception
    if (!isConstraintViolation) {
        // We must manually check the error codes
        Throwable rootThrowable = getRootCause(exception);
        if (rootThrowable instanceof SQLException) {
            SQLException sqlException = (SQLException) rootThrowable;
            isConstraintViolation = POSTGRES_SQL_STATE_CODE_FOREIGN_KEY_VIOLATION.equals(sqlException.getSQLState());
        }
    }
    return isConstraintViolation;
}
Also used : SQLException(java.sql.SQLException) ConstraintViolationException(org.hibernate.exception.ConstraintViolationException)

Aggregations

ConstraintViolationException (org.hibernate.exception.ConstraintViolationException)38 Test (org.junit.Test)16 PersistenceException (javax.persistence.PersistenceException)12 Session (org.hibernate.Session)11 SQLException (java.sql.SQLException)7 RObject (com.evolveum.midpoint.repo.sql.data.common.RObject)4 ObjectAlreadyExistsException (com.evolveum.midpoint.util.exception.ObjectAlreadyExistsException)4 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)4 DataIntegrityViolationException (org.springframework.dao.DataIntegrityViolationException)4 HttpHeaders (org.springframework.http.HttpHeaders)4 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)4 User (ca.corefacility.bioinformatics.irida.model.user.User)3 ObjectNotFoundException (com.evolveum.midpoint.util.exception.ObjectNotFoundException)3 Connection (java.sql.Connection)3 AbstractManyToManyAssociationClassTest (org.hibernate.test.manytomanyassociationclass.AbstractManyToManyAssociationClassTest)3 ResultModels (eu.bcvsolutions.idm.core.api.dto.ResultModels)2 DefaultErrorModel (eu.bcvsolutions.idm.core.api.exception.DefaultErrorModel)2 ErrorModel (eu.bcvsolutions.idm.core.api.exception.ErrorModel)2 ErrorData (io.crnk.core.engine.document.ErrorData)2 PreparedStatement (java.sql.PreparedStatement)2