ThorstenFreie wrote:
Hi,
If the constraint is violated Hibernate throws (as expected) an ConstraintViolationException. But the method getConstraintName () returns null.
If I violate the same constraint direct in the database I get:
test=# insert into clubfunctiontype values (5,'SaveVorsitzender','');
ERROR: duplicate key violates unique constraint "clubfunctiontype_name_key"
Is there a possibilty to get a part of this error message as return value of the getConstraintName method?
Yes, there is. You'll need to either subclass or modify a method in org.hibernate.dialect.PostgreSQLDialect. Of interest to you will be the getViolatedConstraintNameExtracter method. At the moment there's no logic there to detect duplicate key referential integrity violations:
Code:
public ViolatedConstraintNameExtracter getViolatedConstraintNameExtracter() {
return EXTRACTER;
}
private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
public String extractConstraintName(SQLException sqle) {
try {
int sqlState = Integer.valueOf( JDBCExceptionHelper.extractSqlState(sqle)).intValue();
switch (sqlState) {
// CHECK VIOLATION
case 23514: return extractUsingTemplate("violates check constraint "",""", sqle.getMessage());
// UNIQUE VIOLATION
case 23505: return extractUsingTemplate("violates unique constraint "",""", sqle.getMessage());
// FOREIGN KEY VIOLATION
case 23503: return extractUsingTemplate("violates foreign key constraint "",""", sqle.getMessage());
// NOT NULL VIOLATION
case 23502: return extractUsingTemplate("null value in column "","" violates not-null constraint", sqle.getMessage());
// TODO: RESTRICT VIOLATION
case 23001: return null;
// ALL OTHER
default: return null;
}
} catch (NumberFormatException nfe) {
return null;
}
}
};
Hope this helps.
Regards,
Andrei.