Home > Software engineering >  Why am I getting null for the date when I create a Todo entity?
Why am I getting null for the date when I create a Todo entity?

Time:04-11

What is wrong with my to-do application? I want the user to be able to add a todo and have it be saved in my MySQL database with the time it was created, but I don't know what I'm doing wrong.

I am new to learning Springboot and would appreciate any suggestions or advice.

Todo Entity:


import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import java.util.Date;


@Entity(name = "Todo")
@NoArgsConstructor
@Table(name = "todos")
public class Todo {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private int id;

    @Column(name="description")
    private String description;

    @Column(name="target_date")
    @CreationTimestamp
    private Date targetDate;

    public Todo(String description) {
        this.description = description;
    }

    public int getId() {
        return id;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Date getTargetDate() {
        return targetDate;
    }

    public void setTargetDate(Date targetDate) {
        this.targetDate = targetDate;
    }

    @Override
    public String toString() {
        return "Todo{"  
                "id="   id  
                ", description='"   description   '\''  
                ", targetDate="   targetDate  
                '}';
    }
}

Adding a Todo with Spring Data JPA


import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
@Component
public interface TodoRepository extends JpaRepository<Todo, Integer> {

    @Modifying
    @Query(value = "INSERT INTO todos (description) VALUES (:description)", nativeQuery=true)
    @Transactional
    void addTodo(@Param("description") String description);

}

TodoController

@RestController
@RequestMapping(value = "/api/v1/todos")
@AllArgsConstructor
public class TodoController {

    @Autowired
    private ITodoService todoService;

    @PostMapping(value = "/add-todo")
    public String addTodo(@RequestParam String description) {
        Todo todo = new Todo();
        todo.setDescription(description);
        todoService.addTodo(todo);

        return todo.toString();
    }

after getting a post request, the target_date is getting NULL in MySQL

CodePudding user response:

I assume you can solve it by using persist():

@Autowired EntityManager entityManager;

@PostMapping(value = "/add-todo")
    public String addTodo(@RequestParam String description) {
        Todo todo = new Todo();
        todo.setDescription(description);
        entityManager.persist(todo);

        return todo.toString();
    }
  • Related