cause.getConstraintName() 返回一个奇怪的字符串而不是实际的约束名称

cause.getConstraintName() returning a weird string instead of actual constraint name

我有一列设置为 unique = true

这是我实体的代码-

@Entity
@Table(name = "users", uniqueConstraints=[UniqueConstraint(name = "userName", columnNames=["user_name"])])
class UserEntity(@Column(name = "user_id", nullable = false)
                 @Id @GeneratedValue(strategy = GenerationType.AUTO)
                 var id: Long = 0,
                 @Column(name = "user_name", nullable = false, unique = true)
                 @field:NotEmpty(message = "Please provide a user name")
                 var userName: String?)

现在,当由于尝试插入重复项 user_name 而出现 DataIntegrityViolationExceptionConstraintViolationException 时,我尝试像这样提取约束名称的确切名称 -

val failConstraint = (ex.cause as ConstraintViolationException).constraintName

但是,我得到的不是正确的约束名称,而是一个奇怪的字符串 - "uk_k8s0f4n7n77w1a16yhua69ony"

详细留言是这样的-

ERROR: duplicate key value violates unique constraint "uk_k8s0f4n7n77w1a16yhua69ony"\n Detail: Key (user_name)=(krtkush) already exists.

我大致遵循这个答案 -

我哪里错了?

我必须使 IDE 的缓存无效并重新启动它才能工作。代码没问题。