作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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());
}
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
最佳答案
javadoc 说:
Returns the name of the violated constraint, if known.
Returns:
The name of the violated constraint, or null if not known.
关于Hibernate ConstraintViolationException getConstraintName,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5661792/
mysql> alter table metakey add constraint Name unique(name); mysql> desc metakey; +-------+--------
我是一名优秀的程序员,十分优秀!