仅在保存(插入)时进行休眠验证 [英] Hibernate validations on save (insert) only

查看:114
本文介绍了仅在保存(插入)时进行休眠验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们遇到了遗留代码的问题。有一个用于用户名字段的验证集,验证其长度并确保其至少包含一个字母:

  @ Column(name =username)
@Size(min = 4,max = 40)
@Pattern(regexp =^。* [a-zA-Z] +。* $)
私人字符串用户名;

我们遇到的问题是一些现有的遗留数据不适合这些验证,我正在尝试以找到一种方法,使遗留数据(旧用户)忽略这些验证,同时仍然适用于新创建的用户。



我正在考虑移动验证到 setUsername(...)方法(因此值将仅在实际更改时进行验证),但是这导致了一个异常:

  javax.validation.ValidationException:带注释的方法必须遵循JavaBeans命名约定。 setUsername()不。 

我也确定实体设置为 dynamicUpdate = true ,但这并没有帮助,因为即使没有发生变化,hibernate也正在验证所有属性。

如何防止这些验证是在更新过程中在现有实体上执行?

我不希望修订影响同一实体上的其他属性验证,我无法更改休眠配置。

解决方案

经过两天的研究,我发现如何使这项工作。

显然,那么仅在 INSERT 上验证并不困难。所需的唯一更改是将这些验证设置为特定验证组,并在 INSERT / pre-persist events。


首先,我创建了一个名为 platform.persistence.InsertOnlyValidations 的接口,用作一个组这将在pre-persist期间进行验证。



然后,我将该组添加到用户名字段验证中:

  @Column(name =username)
@Size(min = 4,max = 40,groups = {InsertOnlyValidations.class})
@Pattern(regexp =^。* [a-zA-Z] +。* $,groups = {InsertOnlyValidations.class})
private String username;

这指示hibernate不要将这些验证用作默认组的一部分。现在,我需要指示hibernate在插入时验证这些验证规则。

这样做很简单,我需要传递属性 javax.persistence.validation。 group.pre-persist ,同时指示哪些组将在 pre-persist 事件期间被验证:

  javax.persistence.validation.group.pre-persist = javax.validation.groups.Default,platform.persistence.InsertOnlyValidations 

这指示hibernate在 pre-persist 事件期间将验证所有默认验证( InsertOnlyValidations 组中包含的所有验证。 $ b

We encountered a problem with legacy code. There is a validation set for a "username" field, validating its length and making sure it contains at least one letter:

@Column(name = "username")
@Size(min = 4, max = 40)
@Pattern(regexp = "^.*[a-zA-Z]+.*$")
private String username;

The problem we have is that some existing legacy data do not fit these validations, and I'm trying to find a way to make these validations to be ignored for legacy data (old users), while still be applied to newly created users.

I was thinking about moving the validations to setUsername(...) method (so value will be validated on an actual change only), but this caused an exception:

javax.validation.ValidationException: Annotated methods must follow the JavaBeans naming convention. setUsername() does not.

I also made sure the entity is set to dynamicUpdate=true, but this doesn't help since hibernate is validating all properties, even if no change occurred.

How can I prevent these validations to be performed on existing entities during update?
I do not want the fix to impact other properties validations on the same entity and I can't change hibernate configuration.

解决方案

After two days of research I found out how to make this work.
Apparently, specifying validations that would be validated on INSERT only is not that difficult. The only changes required are to set these validations to a specific validation group and to validate this group during INSERT/pre-persist events.

First of all I created an interface called platform.persistence.InsertOnlyValidations to be used as a group which will be validated during pre-persist only.

Than, I added the group to the username field validations:

@Column(name = "username")
@Size(min = 4, max = 40, groups = {InsertOnlyValidations.class})
@Pattern(regexp = "^.*[a-zA-Z]+.*$", groups = {InsertOnlyValidations.class})
private String username;

This instructs hibernate not to use these validations as part of the default group. Now, I needed to instruct hibernate to validate these validation rules during insert only.
The way to do that is very simple, I needed to pass the property javax.persistence.validation.group.pre-persist, while indicating which groups will be validated during a pre-persist event:

javax.persistence.validation.group.pre-persist=javax.validation.groups.Default,platform.persistence.InsertOnlyValidations

This instructs hibernate that during a pre-persist event all default validations will be validated (javax.validation.groups.Default) in addition to all the validations included in the InsertOnlyValidations group.

这篇关于仅在保存(插入)时进行休眠验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆