Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Hibernate只执行java属性的第一个自定义约束_Java_Spring Boot_Hibernate_Repository_Constraints - Fatal编程技术网

Hibernate只执行java属性的第一个自定义约束

Hibernate只执行java属性的第一个自定义约束,java,spring-boot,hibernate,repository,constraints,Java,Spring Boot,Hibernate,Repository,Constraints,我正在使用Hibernate为我的java模型创建自定义约束注释。调用每个约束时,将返回一条特定的API错误消息和400个错误请求作为HTTP状态代码。 每个约束的响应:- { "errorMsg": "revoked id", "errorCode": 400 }, { "errorMsg": "paused id", "errorCode": 400 }, { &quo

我正在使用Hibernate为我的java模型创建自定义约束注释。调用每个约束时,将返回一条特定的API错误消息和400个错误请求作为HTTP状态代码。 每个约束的响应:-

{
"errorMsg": "revoked id",
"errorCode": 400
},

{
"errorMsg": "paused id",
"errorCode": 400
},

{
"errorMsg": "expired id",
"errorCode": 400
}
这些是创建的自定义约束
ExistingExpiredId
ExistingPauseId
ExistingRevokedId
:-

@NotBlank
@ExistingExpiredId
@ExistingPausedId
@ExistingRevokedId
private String id;
ID可以具有以下状态:-
过期、暂停、吊销
。目前,我有自定义注释,用于检查属性的不同状态。每个自定义约束对数据库进行存储库调用,以选择属性所处的特定状态。我看到所有的约束都被调用了,因此对数据库进行了3次调用,这对于单个属性来说是非常昂贵的。如何确保一次只调用三个约束中的一个,而不是全部三个?
hibernate
是否提供了实现此功能的机制?

是排序验证约束的标准方法。唯一需要注意的是,您需要将每个约束放在一个单独的组中

如果我是您,我将创建一个参数化的约束注释(带有
Status[]value()
属性),用于检查具有给定
id
state
s的对象是否存在,然后可以按如下方式使用:

@ExistingId(State.EXPIRED) // check if an expired entity exists

@ExistingId(State.PAUSED) // check if a paused entity exists

@ExistingId({State.EXPIRED, State.PAUSED, State.REVOKED}) // check if an entity exists in any of the listed statuses
如果需要,可以将状态配置的某些版本别名如下:

@Constraint(validatedBy = {})
@Target(...)
@Retention(RUNTIME)
@ExistingId(Status.EXPIRED)
public @interface ExistingExpiredId {...}

也许您可以考虑将@ConstraintComposition(或)与@ReportAssingeViolation结合使用,以减少开销