Unique constraint violation handler no longer working

Hi, I recently migrated to Cuba Studio 6.7.7 and Platform 6.7-SNAPSHOT and my unique constraint violation handler is no longer working. Did framework change? These were working before the migration.

I followed the instructions from the following threads:
https://www.cuba-platform.com/discuss/t/unique-index-custom-exception-handler/963
https://www.cuba-platform.com/discuss/t/validation-message-for-unique-constraint/994/3

Studio creates the following index:
create unique index IDX_SOMETHING_COMPANY_UNIQ_COMPANY_NAME on SOMETHING_COMPANY (COMPANY_NAME) ^

So I put the following in my web messages.properties file:
IDX_SOMETHING_COMPANY_UNIQ_COMPANY_NAME = Company Name already exists

But I no longer get the pretty message on the screen. Now I just get the error:
Unique constraint violation occurred
Please note this message is in the middle of the screen and does not include the index name in parenthesis.

If this is still how the framework works, then any ideas or suggestions on how to debug the problem would be helpful.

Thanks.

Hi,

It should work as before. You can find out what is going on in your case if you set a breakpoint to com.haulmont.cuba.gui.exception.UniqueConstraintViolationHandler#doHandle method and connect with the debugger.

Hi,

The constraint name is not found in the doHandle method. The group count of the matcher is 2. So the code block skips the logic where the constraint name is set. Perhaps MySQL is throwing content that the matcher is not expecting?

Here is matcher.toString():

java.util.regex.Matcher[pattern=Duplicate entry '(.+)' for key '(.+)' region=0,2862 lastmatch=Duplicate entry 'Moes' for key 'IDX_DEMO_COMPANY_UNIQ_COMPANY_NAME']

Here is the throwable.toString()

com.haulmont.cuba.core.global.RemoteException:
---
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.cuba20): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Moes' for key 'IDX_DEMO_COMPANY_UNIQ_COMPANY_NAME'
Error Code: 1062
Call: INSERT INTO DEMO_COMPANY (ID, IS_COMPETITOR, CREATE_TS, CREATED_BY, DESCRIPTION, EXTERNAL_ID, COMPANY_NAME, UPDATE_TS, UPDATED_BY, VERSION, EXTERNAL_SYSTEM_ID, PARENT_COMPANY_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	bind => [ad3d3e11d4ce837fc4b4f7150ab12b23, false, 2018-03-02 16:49:04.539, admin, null, null, Moes, 2018-03-02 16:49:04.539, null, 1, dbc8860b6c2a4f7aa4a440e10a855b3a, null]
Query: InsertObjectQuery(com.rhinoj.demo.entity.Company-ad3d3e11-d4ce-837f-c4b4-f7150ab12b23 [new,managed])
---
javax.persistence.RollbackException: Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.6.2.cuba20): org.eclipse.persistence.exceptions.DatabaseException
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Moes' for key 'IDX_DEMO_COMPANY_UNIQ_COMPANY_NAME'
Error Code: 1062
Call: INSERT INTO DEMO_COMPANY (ID, IS_COMPETITOR, CREATE_TS, CREATED_BY, DESCRIPTION, EXTERNAL_ID, COMPANY_NAME, UPDATE_TS, UPDATED_BY, VERSION, EXTERNAL_SYSTEM_ID, PARENT_COMPANY_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	bind => [ad3d3e11d4ce837fc4b4f7150ab12b23, false, 2018-03-02 16:49:04.539, admin, null, null, Moes, 2018-03-02 16:49:04.539, null, 1, dbc8860b6c2a4f7aa4a440e10a855b3a, null]
Query: InsertObjectQuery(com.rhinoj.demo.entity.Company-ad3d3e11-d4ce-837f-c4b4-f7150ab12b23 [new,managed])
---
org.eclipse.persistence.exceptions.DatabaseException: 
Internal Exception: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Moes' for key 'IDX_DEMO_COMPANY_UNIQ_COMPANY_NAME'
Error Code: 1062
Call: INSERT INTO DEMO_COMPANY (ID, IS_COMPETITOR, CREATE_TS, CREATED_BY, DESCRIPTION, EXTERNAL_ID, COMPANY_NAME, UPDATE_TS, UPDATED_BY, VERSION, EXTERNAL_SYSTEM_ID, PARENT_COMPANY_ID) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
	bind => [ad3d3e11d4ce837fc4b4f7150ab12b23, false, 2018-03-02 16:49:04.539, admin, null, null, Moes, 2018-03-02 16:49:04.539, null, 1, dbc8860b6c2a4f7aa4a440e10a855b3a, null]
Query: InsertObjectQuery(com.rhinoj.demo.entity.Company-ad3d3e11-d4ce-837f-c4b4-f7150ab12b23 [detached])
---
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Moes' for key 'IDX_DEMO_COMPANY_UNIQ_COMPANY_NAME'

Hi Keith,

I’ve found a silly mistake in the UniqueConstraintViolationHandler code, where we iterate over groups if their count is greater than 1. So it couldn’t work for MySQL from the beginning. What confuses me is that you say that it worked on a previous version. What was that version?

Hi Konstantin,

I was on Studio 6.6.1 and Platform 6.6.2. But perhaps I was mistaken in my original assessment. Along the way I migrated from HSQLDB to MySQL and I probably never retested the violation handler in between the database migration and the Cuba upgrade.

Thanks for looking into this. When will a fix be available?

Keith

Thank you for the information. I’ve created an issue, it will be fixed for releases 6.7 and up.

At the moment, you can easily work around the issue if you additionally specify the following app property in your web-app.properties file:

cuba.uniqueConstraintViolationPattern = Duplicate entry '.+' for key '(.+)'

Thank you very much. This worked great.