Lets say I have table defined as:
CREATE TABLE ITEMS(
ID BIGINT PRIMARY KEY,
NAME VARCHAR2,
CONSTRAINT NAME_IS_UNIQUE UNIQUE (NAME)
);
Important part is NAME_IS_UNIQUE
constraint.
With corresponding POJO Item as:
class Item{
private Long id;
private String name;
/** getters and setters */
}
And SQL-Object interface with methods defined as:
@SqlUpdate("insert into items(id, name) values(:id, :name)")
int insert(@BindBean Item itemToInsert);
If I'll try to insert into ITEMS with already existing NAME then I will get DB vendor specific SQLException about constraint NAME_IS_UNIQUE violation.
Is there a way to provide mapping between SQLException and application specific Exception (for example ItemNameUniqueConstraintException) so insert
method essentially changed it signature to something like the one below?
@SqlUpdate("insert into items(id, name) values(:id, :name)")
int insert(@BindBean Item itemToInsert) throws ItemNameUniqueConstraintException;
Question is not about specific UNIQUE constraint, but more about general case, where SQLException can be about anything: Like referential integrity violation or check constraint violation, etc.