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