Date not accepted in Mysql database

Asked

Viewed 167 times

0

I have a doubt because I am developing a web application using the Spring Boot, I have my table in the bank Mysql ready and working normally but not saving the date appears this error on the console.

Field error in object 'tarefas' on field 'data': rejected value [2017-10-22]; codes [typeMismatch.tarefas.data,typeMismatch.data,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tarefas.data,data]; arguments []; default message [data]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'data'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @javax.persistence.Column @javax.persistence.Temporal java.util.Date] for value '2017-10-22'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'tarefas' on field 'data_realizacao': rejected value [2017-10-27]; codes [typeMismatch.tarefas.data_realizacao,typeMismatch.data_realizacao,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tarefas.data_realizacao,data_realizacao]; arguments []; default message [data_realizacao]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'data_realizacao'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @javax.persistence.Column @javax.persistence.Temporal java.util.Date] for value '2017-10-27'; nested exception is java.lang.IllegalArgumentException]
2017-10-23 01:08:44.606  WARN 7596 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors
Field error in object 'tarefas' on field 'data': rejected value [2017-10-08]; codes [typeMismatch.tarefas.data,typeMismatch.data,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tarefas.data,data]; arguments []; default message [data]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'data'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @javax.persistence.Column @javax.persistence.Temporal java.util.Date] for value '2017-10-08'; nested exception is java.lang.IllegalArgumentException]
Field error in object 'tarefas' on field 'data_realizacao': rejected value [2017-10-25]; codes [typeMismatch.tarefas.data_realizacao,typeMismatch.data_realizacao,typeMismatch.java.util.Date,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [tarefas.data_realizacao,data_realizacao]; arguments []; default message [data_realizacao]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'data_realizacao'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotNull @javax.persistence.Column @javax.persistence.Temporal java.util.Date] for value '2017-10-25'; nested exception is java.lang.IllegalArgumentException]

Dry my persistence class here

/**
     * classe modelo persistencia tarefas
     */
package com.web.Models;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;

@Entity
@Table(name = "tarefas")
public class Tarefas implements Serializable{

    private static final long serialVersionUID = 5686959667099623912L;

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    @NotNull
    @Column(name="data")
    @Temporal(TemporalType.TIMESTAMP)
    private Date data;

    @NotNull
    @Column(name="nome")
    private String nome;

    @NotNull
    @Column(name="descricao")
    private String descricao;

    @NotNull
    @Column(name="data_realizacao")
    @Temporal(TemporalType.TIMESTAMP)
    private Date data_realizacao;

    public Tarefas() {

    }

    public Tarefas(long id, Date data, String nome, String descricao, Date data_realizacao) {
        this.id = id;
        this.data = data;
        this.nome = nome;
        this.descricao = descricao;
        this.data_realizacao = data_realizacao;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getData() {
        return data;
    }

    public void setData(Date data) {
        this.data = data;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

    public Date getData_realizacao() {
        return data_realizacao;
    }

    public void setData_realizacao(Date data_realizacao) {
        this.data_realizacao = data_realizacao;
    }





}

And here’s my form HTML

<h1>Cadastro de Tarefas:</h1>
    <form method="post">
        Data:<input type="date" value="" name="data"/>
        Data Termino:<input type="date" value="" name="data_realizacao"/>
        Descricao:<textarea rows="4" cols="50" name="descricao"></textarea>
        Nome:<input type="text" value="" name="nome"/>
        <button type="submit">Salvar</button>

        <h2><a href="/todasTarefas">ver todas as tarefas</a></h2>

    </form>

What might be this I thought it was the date type put in the persistence class with the bank I use the @Temporal(TemporalType.TIMESTAMP), I already searched here on the site I found no help.

  • The format you are probably using may be incorrect. Try using this way: yyyy-mm-dd h:m:s.

  • Yes more I would change this in html in my persisitencia class or in the bank?

  • If you are not using date-only time, the ideal would be for you to change from TIMESTAMP to DATE.

1 answer

0


I managed to solve used:

@DateTimeFormat(pattern = "yyyy-MM-dd") 

in my class.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.