[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-2932) No ConstraintName when using PostgreSQL (BatchUpdateException instead of PSQLException)

Jon Alvarado (JIRA) noreply at atlassian.com
Wed Jun 18 19:35:33 EDT 2008


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-2932?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_30465 ] 

Jon Alvarado commented on HHH-2932:
-----------------------------------

This is still an issue it hasn't been resolved.  The problem is that when you are doing an update operation and a constraint violation happens then the exception that hibernate tries to pull the constraintName from is the BatchUpdateException which does not contain the root cause exception text.  You can see in the JDBCExceptionHelper.extractSqlState(sqle) it does this

<code>
	public static String extractSqlState(SQLException sqlException) {
		String sqlState = sqlException.getSQLState();
		SQLException nested = sqlException.getNextException();
		while ( sqlState == null && nested != null ) {
			sqlState = nested.getSQLState();
			nested = nested.getNextException();
		}
		return sqlState;
	}
</code>

There it calls getNextException to get the correct exception sqlState, but then when the message is processed the wrong one is used.

There should be another method on  JDBCExceptionHelper to get the nested SQL state exception and then that should be used to pull the message off of.




>  No ConstraintName when using PostgreSQL (BatchUpdateException instead of PSQLException)
> ----------------------------------------------------------------------------------------
>
>                 Key: HHH-2932
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-2932
>             Project: Hibernate3
>          Issue Type: Sub-task
>    Affects Versions: 3.2.2
>         Environment: Postgres 8.2
>            Reporter: Florian Rock
>            Assignee: Diego Plentz
>            Priority: Minor
>
> > When using dialect org.hibernate.dialect.PostgreSQLDialect and a ConstraintViolationException is thrown, getConstraintName() does not get the right constraint name. 
> > It seems that there is something wrong with the ConstraintNameExtractor.
> I still run into this problem.
> lets take a look into the PostgresDialect:
> ------ BOF ------
> 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;
> 			}
> 		}
> 	};
> ------ EOF ------
> My problem ist that sqle is a BatchUpdateException and not the suspected PSQLExpection.
> BatchUpdateException message doesn't contain the constraintName!
> I sloved that by adding
>  "SQLException psqle = sqle.getNextException();"
> and replace the "sqle.getMesage()" with "psqle.getMessage()"

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://opensource.atlassian.com/projects/hibernate/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        



More information about the hibernate-issues mailing list