2
如何讓數據庫領域引起ConstraintViolationException同時插入到數據庫中休眠的名稱。如何從ConstraintViolationException獲取數據庫字段名稱 - Hibernamte
我的表像
mysql> desc Mytable;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | bigint(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | UNI | NULL | |
| city | varchar(20) | YES | UNI | NULL | |
+-------+-------------+------+-----+---------+----------------+
在礦井表記錄
mysql> select * from Mytable;
+----+--------+-------+
| id | name | city |
+----+--------+-------+
| 1 | SATISH | BLORE |
+----+--------+-------+
1 row in set (0.00 sec)
現在,我嘗試插入
"RAMESH","BLORE" through hibernate
。
它扔ConstraintViolationException due to "BLORE" (CITY) already Exist
。
如果我嘗試插入。
"SATISH","MLORE" through hibernate
它扔ConstraintViolationException due to "SATISH" (NAME) already Exist
。
我的問題是 如何讓字段名誰在通過Hibernate導致異常ConstraintViolationException。
謝謝托馬斯,我試着用它給NULL getConstraintName()方法。 – sateesh 2012-02-06 13:59:36
@sateesh嗯,這可能是一個MySQL方言的問題。 AFAIK'MySQLDialect'不會覆蓋'getViolatedConstraintNameExtracter()'方法,因此返回一個總是給出'null'作爲約束名的提取器,然後傳遞給異常。在這種情況下,您似乎需要手動從嵌套的'SQLException'中獲取約束名稱。 – Thomas 2012-02-06 14:50:43