gpt4 book ai didi

java - MySQLIntegrityConstraintViolationException 未在异常消息中显示列名

转载 作者:行者123 更新时间:2023-11-29 18:06:26 35 4
gpt4 key购买 nike

我将 JPAHibernateMySQL 一起使用。

我想要的是捕获MySQLIntegrityConstraintViolationException来获取unicity约束违规,并显示哪个字段被违规,从异常消息中获取字段名称,但它只显示我用一些奇怪的哈希值代替列名。

我的映射只是一些默认的映射:

@Entity
@Table(name = "myentity")
public class SomeEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Column(unique = true, nullable = false, name = "somefield")
@NotNull
private String somefield;
}

异常(exception)

caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry '54533355000175' for key 'UK_bg2tfjjhyqxens7cw8a4uhscw'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
at com.mysql.jdbc.Util.getInstance(Util.java:408)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3973)

我想要解析的消息是:

Duplicate entry '54533355000175' for key 'UK_bg2tfjjhyqxens7cw8a4uhscw'

如何将此键 'UK_bg2tfjjhyqxens7cw8a4uhscw' 转换为正确的列 somefield 名称?我可以在某处添加一些配置来完成此任务吗?

最佳答案

我设法做到了 @Billy Frost使用 @Table uniqueConstraints 注释字段指出。

@Entity
@Table(name = "myentity", uniqueConstraints = {
@UniqueConstraint(name = "theNameIWant"), columnNames={"somefield"},
... other constraints ...
}
)
public class SomeEntity implements Serializable {

private static final long serialVersionUID = 1L;

@Column(unique = true, nullable = false, name = "somefield")
@NotNull
private String somefield;
}

关于java - MySQLIntegrityConstraintViolationException 未在异常消息中显示列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47776410/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com