【问题标题】:Error with validator (No validator could be found for type: java.util.Date.)验证器出错(找不到类型的验证器:java.util.Date。)
【发布时间】:2013-05-09 11:24:11
【问题描述】:

朋友们,

我和...上课

@NotBlank(message = "timesheet.cadastro.horainicio.obrigatorio")
@Temporal(TemporalType.TIME)
@Column(name = "INICIO", nullable = false)
private Date horaInicio;

而且,在我的测试(groovy)中,我将 null 放入“horaInicio”:

def "Salvar um timesheet sem hora inicio"() {
    given:"um timesheet sem data"
        def usuario = sessionFactory.getCurrentSession().get(Usuario.class,1L) as Usuario
        def dias = sessionFactory.getCurrentSession().get(Dias.class,1L) as Dias
        def timeSheet = criarTimeSheet(usuario,dias) as TimeSheet
        timeSheet.horaInicio = null
    when:"buscar na base"
        def timeSheetPersistido = timeSheetService.salvar(timeSheet)
    then:"retorna uma lista de timesheet"
        def erro = thrown(ConstraintViolationException)
        "timesheet.cadastro.horainicio.obrigatorio".equals(erro.getConstraintViolations().asList().get(0).getMessage())
}

但我有错误:

Expected exception javax.validation.ConstraintViolationException, but got javax.validation.UnexpectedTypeException
    at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:79)
    at br.com.infowhere.service.TimeSheetServiceIt.Salvar um timesheet sem hora inicio(TimeSheetServiceIt.groovy:80)
Caused by: javax.validation.UnexpectedTypeException: HV000030: No validator could be found for type: java.util.Date.

谁能帮帮我?

谢谢

【问题讨论】:

    标签: hibernate


    【解决方案1】:
    @Temporal(DATE)
    @Column(name = "creation_timestamp")
    private Date creationTimestamp;
    

    @Temporal(TIMESTAMP)
    @Column(name = "creation_timestamp")
    private Date creationTimestamp;
    

    如果数据库类型是“日期时间”。

    简单而且有效。不需要额外的@annotations。

    【讨论】:

      【解决方案2】:

      只需删除@Temporal(TemporalType.TIME) 注释;-)

      【讨论】:

      • 为什么?解释。问候。
      • 取决于 db,为什么 mysql 删除 @Temporal 将与 util.Date 一起使用。
      【解决方案3】:

      正如文档中所说,@NotBlank 用于字符串类型。没有 java.util.Date 为空白的概念。它可以为空或不为空。

      验证带注释的字符串不为 null 或为空。这 与 NotEmpty 的区别在于尾随空格越来越 忽略。

      如果目标是检查值不为空,则应使用@NotNull。根据文档:

      带注释的元素不能为空。接受任何类型。

      【讨论】:

      • 我放了@NotNull 但不起作用:-( @NotNull(message = "timesheet.cadastro.horrainicio.obrigatorio") @Temporal(TemporalType.TIME) @Column(name = "INICIO" , nullable = false) 私有日期 horaInicio;
      • 嗨,我的朋友,对不起,现在可以工作了!我还有一个没有改变的属性 ;-) 谢谢!!!
      猜你喜欢
      • 2014-11-16
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 2012-06-01
      • 2019-03-23
      • 2023-01-05
      相关资源
      最近更新 更多