2013-05-15 12 views
7

Moi przyjaciele,Błąd weryfikatora (Nie walidator można znaleźć na typ. Java.util.Date)

mam klasę ...

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

A w moim teście (Groovy) kładę 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()) 
} 

ale mam błąd:

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. 

Ktoś może mi pomóc?

dzięki

Odpowiedz

25

powiedział jak również w dokumentacji @NotBlank jest na typ String. Nie ma koncepcji java.util.Date jest pusta. Może mieć wartość NULL lub nie mieć wartości NULL.

Validate that the annotated string is not null or empty. The difference to NotEmpty is that trailing whitespaces are getting ignored.

Jeśli celem jest sprawdzenie, czy wartość nie jest pusta, należy użyć @NotNull. Według dokumentacji:

The annotated element must not be null. Accepts any type.

+0

kładę @NotNull ale nie działa :-(@NotNull (message = "timesheet.cadastro.horainicio.obrigatorio") @Temporal (TemporalType.TIME) @Column (name = "INICIO" pustych = false) prywatne Data horaInicio; – user812612

+0

cześć mój przyjacielu, przepraszam, to działa teraz! Miałem inny atrybut, że się nie zmieniłem ;-) dzięki! – user812612

-2

Juste usuwać @Temporal(TemporalType.TIME) adnotacji ;-)

+0

Dlaczego? Wyjaśniać. Pozdrowienia. –

+0

zależy od db, dlaczego mysql usuwanie @Temporal będzie działać z util.Date. –

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

lub

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

jeśli db typ jest 'datetime'.

Prosty i działa. nie było potrzeby dodatkowych nagród.

Powiązane problemy