2013-05-15 32 views
7

我的朋友們,誤差校驗(無驗證可以爲類型中找到:java.util.Date)

我有類...

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

而且,在我的測試(常規)我把空到 「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. 

有人可以幫助我嗎?

感謝

回答

25

在文檔作爲還表示,@NotBlank是字符串類型。沒有java.util.Date的概念是空白的。它可以爲null或不爲null。

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

如果目標是檢查值是否爲空,則應使用@NotNull。根據文檔:

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

+0

我把@NotNull但不:-(@NotNull(消息= 「timesheet.cadastro.horainicio.obrigatorio」) @Temporal(TemporalType.TIME) @Column(name = 「INICIO」 工作,可爲空=假) 私人日期horaInicio; – user812612

+0

嗨,我的朋友,我很抱歉,現在工作!我有另一個屬性,我沒有改變;-)謝謝! – user812612

-2

中庸之道刪除@Temporal(TemporalType.TIME) 註釋;-)

+0

爲什麼?說明。問候。 –

+0

依賴於db,爲什麼刪除@Temporal的mysql將與util.Date一起使用。 –

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

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

如果數據庫類型是 '日期時間'。

簡單而有效。不需要額外的@annotations。

相關問題