Home > Net >  Finding the name of the column in which duplicate value was inserted
Finding the name of the column in which duplicate value was inserted

Time:08-01

In hibernate for columns annotated with @Column(unique = true) we get an exception when trying to insert a duplicate value for that column.

2022-07-31 19:47:18.112 ERROR 14372 --- [io-8080-exec-10] o.h.engine.jdbc.spi.SqlExceptionHelper   : Duplicate entry ' someValue' for key 'business.UK_3h6o7iww8015m4q3yffbkqtrw'

So from the above log, I know that business is the table in which I am inputting a duplicate value.

Is there a way to know in which column the duplicate value is getting inserted into? Since a single table can have multiple unique columns.

CodePudding user response:

In addition to what Davide D'Alto wrote, you can also enable SQL logging, e. g. as shown here.

CodePudding user response:

You can check the SQL for the table creation:

show create table business;

or you can run the following query:

select COLUMN_NAME
from information_schema.KEY_COLUMN_USAGE
where TABLE_NAME = 'business' and CONSTRAINT_NAME='UK_3h6o7iww8015m4q3yffbkqtrw'
  • Related