开发者

Hibernate ConstraintViolationException getConstraintName

mysql> alter table metakey add constraint Name unique(name);
mysql> desc  metakey;
+-------+---------------------+------+-----+---------+----------------+
| Field | Type                | Null | Key | Default | Extra          |
+-------+---------------------+------+-----+---------+----------------+
| id    | bigint(20) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(45)         | NO   | UNI |         |                |
+-------+---------------------+------+-----+---------+----------------+


@Entity
@Table(name = "metakey",uniqueConstraints={@UniqueConstraint(name="Name",columnNames={"name"})})
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class MetaKey implements Serializable{
    @Id @Column @GeneratedValue private Long id;
    @Column private String name;
}


sess.save(obj);
 ..
}catch(ConstraintViolationException cve){
  log.error(cve.getMessage());
  log.info("Constraint name:"+cve.getConstraintName()开发者_JAVA百科);
} 

I get these in log - cve.getConstraintName() returns null

org.hibernate.util.JDBCExceptionReporter  - SQL Error: 1062, SQLState: 23000
org.hibernate.util.JDBCExceptionReporter  - Duplicate entry 'Unit' for key 'Name'

could not insert: [com.comp.MetaKey]
Constraint name:null

Is there any way to find the constraint name?

Server version: 5.1.56 Hibernate version:3.6


The javadoc says :

Returns the name of the violated constraint, if known.

Returns:
    The name of the violated constraint, or null if not known.

So I guess the MySQL driver doesn't give access to the violated constraint name in the SQLException it generated, or Hibernate doesn't know how to get it from the SQLException.

Look at the details of the SQLException using a debugger, and see if it can be extracted from somewhere. If it can, then try extending the dialect in order to extract the constraint name.


Please check out me answering almost the same question: Hibernate: constraintName is null in MySQL

Hope this helps, Alex.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜