我的朋友,
我上课了......
@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.
任何人都可以帮助我?
感谢
答案 0 :(得分:28)
正如文档中所述,@NotBlank用于String类型。没有java.util.Date的概念为空。它可以为null或不为null。
验证带注释的字符串不为null或为空。该 与NotEmpty的区别在于尾随空格正在增加 忽略。
如果目标是检查值是否为空,则应使用@NotNull。根据文件:
带注释的元素不能为null。接受任何类型。
答案 1 :(得分:0)
@Temporal(DATE)
@Column(name = "creation_timestamp")
private Date creationTimestamp;
或
@Temporal(TIMESTAMP)
@Column(name = "creation_timestamp")
private Date creationTimestamp;
如果db类型是'datetime'。
简单而且有效。不需要额外的@annotations。
答案 2 :(得分:-2)
juste删除@Temporal(TemporalType.TIME)
注释; - )