2011-04-14 3 views
1
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()); 
} 

내가 이러한 로그에 얻을 - cve.getConstraintName을()는 제약 조건 이름을 찾을 수있는 방법이최대 절전 모드 ConstraintViolationException 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 

거기에 null을 반환?

서버 버전 : 5.1.56 최대 절전 모드 버전 : 3.6

+0

동일한 문제가 있습니다. – dmonti

답변

1

javadoc의 말씀 : 알려진 경우

이 위반 된 제약 조건의 이름을 돌려줍니다.

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

그래서 나는 MySQL의 드라이버가 생성되는 SQLException의 위반 제약 조건 이름에 대한 액세스 권한을 부여하지 않는 생각, 또는 최대 절전 모드가되는 SQLException에서 그것을 얻을하는 방법을 알고하지 않습니다.

디버거를 사용하여 SQLException의 세부 사항을보고 어딘가에서 추출 할 수 있는지 확인하십시오. 가능한 경우 제약 조건 이름을 추출하기 위해 방언을 확장 해보십시오.

+1

cve.getCause(). getMessage()는 "Name '키에 중복'Unit '항목을 반환합니다."Name "은 제약 조건 이름입니다. – yodhevauhe