我想存储birthdate所以我选择date了MySQL,当我在我的数据库中创建我的实体时,结果如下:
import java.util.Date;
    // ..code
    @NotNull(message="fill you birthdate")
    @Temporal(TemporalType.DATE)
    private Date birthdate;
但是当我试图坚持它时,它给了我这个错误:
在回调事件上执行自动Bean验证时违反了Bean验证约束:'prePersist'.有关详细信息,请参阅嵌入式ConstraintViolations.
我在这做错了什么?我正在阅读有关谷歌定义时区的内容,我来自巴西,我应该怎么做?
编辑
package entity;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.hibernate.validator.constraints.Email;
import java.util.Date;
import java.util.List;
/**
 * The persistent class for the user database table.
 * 
 */
@Entity
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Integer id;
    @Temporal(TemporalType.DATE)
    private Date birthdate;
    @NotNull(message="informe seu e-mail")
    @Email(message="e-mail inválido")
    private String email;
    @NotNull(message="informe …如何设置一个bean验证约束,它List应该至少包含1并且最多包含10个元素?
以下都不起作用:
@Min(1)
@Max(10)
@Size(min=1, max=10)
private List<String> list;
我正在尝试在我的注释中包含一个动态消息,该消息根据传递给它的其他变量中的值来更改文本的主体.我设置了默认消息,但是当设置了某个指示符时,我想显示不同的消息.这可能吗?
这是我的注释 -
@Target({TYPE, ANNOTATION_TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch
{
    Class<?>[] groups() default {};
    Class<? extends Payload>[] payload() default {};
    String first();
    String second();
    String third() default "";
    String match() default "true";
    String message() default "{error.theseValuesDontMatch}";
    /**
     * Defines several <code>@FieldMatch</code> annotations on the same element
     *
     * @see FieldMatch
     */
    @Target({TYPE, ANNOTATION_TYPE})
    @Retention(RUNTIME)
    @Documented @interface List
    {
        FieldMatch[] value();
    }
}
这是注释使用的验证器类 -
public class FieldMatchValidator implements ConstraintValidator<FieldMatch, Object>
{
    private String firstFieldName;
    private String …我在我的网络应用程序中使用Hibernate验证器进行表单验证.我在我的String属性中使用@Length注释,如下所示:
@Length(min = 5, message = "The field must be at least 5 characters")
private String myString;
但是,如果String超过50个字符,我需要显示不同的消息.有没有办法使用开箱即用的@Length验证器来做到这一点?我想做的一个例子(编译器不会让我)如下:
@Length(min = 5, message = "The field must be at least 5 characters")
@Length(max = 50, message = "The field must be less than 50 characters")
private String myString;
我试过@Max和@Min但他们没有做我想做的事.任何投入将不胜感激!
我在bean中有一个变量名.我想添加@Pattern验证只接受字母数字.
目前,我有这个.
 @NotNull
 @Pattern(regexp = "{A-Za-z0-9}*")
 String name;
但错误是Invalid regular expression.
我尝试过[A-Za-z0-9].但这也不起作用.虽然没有错误.它将任何有效输入显示为失败.
所以首先我想不出这个问题的更好的标题,所以我愿意接受改变.
我正在尝试使用带有spring boot的bean验证机制(JSR-380)来验证bean.
所以我有一个像这样的控制器:
@Controller
@RequestMapping("/users")
class UserController {
    @PostMapping
    fun createUser(@Validated user: User, bindingResult: BindingResult): ModelAndView {
        return ModelAndView("someview", "user", user)
    }
}
这是用kotlin编写的User类:
data class User(
    @field:NotEmpty
    var roles: MutableSet<@NotNull Role> = HashSet()
)
这是测试:
@Test
internal fun shouldNotCreateNewTestWithInvalidParams() {
    mockMvc.perform(post("/users")
        .param("roles", "invalid role"))
        .andExpect(model().attributeHasFieldErrors("user",  "roles[]"))
}
无效角色映射为null.
如您所见,我想要roles包含至少一个项目,其中没有项目为null.但是,在测试上述代码时,如果roles包含空值,则不会报告绑定错误.如果集合为空,它会报告错误.我认为这可能是kotlin代码如何编译的问题,因为当用java编写User类时,相同的代码工作得很好.像这样:
@Data // just lombok...
public class User {
    @NotEmpty
    private Set<@NotNull Role> roles = new HashSet<>();
}
同一控制器,相同的测试.
在检查字节码后,我注意到kotlin版本不包括嵌套@NotNull注释(见下文).
Java的:
private Ljava/util/Set; roles …我正在尝试添加约束检查,如下所述如何在EclipseLink/JPA中指定@OneToMany的基数
比方说,我有一个字段,user_name在表格中应该是唯一的.
使用Spring/Hibernate验证验证它的最佳方法是什么?
我有一个非常简单的Spring Boot应用程序,它使用Spring-Data-Mongodb
我想要做的就是设置一个JSR-303验证规则,说明我保存的对象必须有一个用户名.我读到JSR-303被添加到版本1.1中的spring-data-mongodb中,所以我假设当我保存一个对象时它已经过验证,但事实并非如此.
有没有人有一个简单的示例设置,显示它是如何工作的?
我的用户pojo看起来像
public class User {
    @Id
    private String id;
    @NotNull(message = "User Name is compulsory")
    private String userName;
    private String password;
    public User() {}
    public String getId() {
      return id;
    }
    public void setId(String id) {
      this.id = id;
    }
    public String getUserName() {
      return userName;
    }
    public void setUserName(String userName) {
      this.userName = userName;
    }
    public String getPassword() {
      return password;
    }
    public void setPassword(String password) {
      this.password = PasswordAuthService.hash(password);
    }
}
我看到某个地方,如果你在上下文中创建了一个验证器,验证只会启动,所以我尝试更新我的Application类(包含所有配置,看起来像
@Configuration
@ComponentScan …在我的 Spring Boot 项目中,我有两个 DTO 正在尝试验证,LocationDto 和 BuildingDto。LocationDto 有一个 BuildingDto 类型的嵌套对象。
这些是我的 DTO:
位置Dto
public class LocationDto {
  @NotNull(groups = { Existing.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;
  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;
  @NotNull(groups = { New.class, Existing.class, LocationGroup.class })
  @Getter
  @Setter
  private BuildingDto building;
  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;
}
建筑Dto
public class BuildingDto {
  @NotNull(groups = { Existing.class, LocationGroup.class })
  @Null(groups = …bean-validation ×10
java ×7
spring-boot ×3
jpa ×2
spring ×2
annotations ×1
eclipselink ×1
hibernate ×1
java-ee ×1
jpa-2.0 ×1
jsr380 ×1
kotlin ×1
mongodb ×1
regex ×1