2013-05-15 3 views
7

Meine Freunde,Fehler mit Validator (No Validator könnte für Typ gefunden werden:. Java.util.Date)

Ich habe Klasse mit ...

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

Und in meinem Test (groovy) ich habe 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()) 
} 

aber ich habe Fehler:

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. 

Jeder kann mir helfen?

dank

Antwort

25

Wie gesagt auch in der Dokumentation, @NotBlank für String-Typ ist. Es gibt kein Konzept von java.util.Date, das leer ist. Es kann null oder nicht null sein.

Wenn Ziel zu prüfen ist, ist Wert nicht null, @NotNull sollte verwendet werden. Laut Dokumentation:

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

+0

Ich habe @NotNull aber nicht funktionieren :-(@NotNull (message = "timesheet.cadastro.horainicio.obrigatorio") @Temporal (TemporalType.TIME) @Column (name = "INICIO", nullable = falsch) private Datum horaInicio; – user812612

+0

Hallo mein Freund, es tut mir leid, es ist Arbeit! Ich hatte ein anderes Attribut, das ich nicht geändert hatte ;-) danke !!! – user812612

-2

juste löschen @Temporal(TemporalType.TIME) Anmerkung ;-)

+0

Warum? Erklären. Grüße. –

+0

hängt von db ab, warum mysql das Entfernen von @Temporal mit einem util.Date funktioniert. –

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

oder

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

wenn db Typ 'Datetime'.

Einfach und es funktioniert. keine zusätzlichen @annotations benötigt.

Verwandte Themen