0

I use Spring Boot annotaion @Entity to auto-create database fields. So, i've tried this

@Entity
@Table
@Data
public class Application {
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private Date date;
}

But Spring Boot makes 'date' typeof DATETIME in MySQL, so it's add time (00:00:00) to my row. How can I fix this?

And another question. I have User.class

@Entity
@Table
@Data
public class User implements UserDetails {

    private String username;
    private String password;
    private String first_name;
    private String last_name;

}

But every field of this is nullable, so should I add an annotation @Column(nullable = false) to every field to makes it not null? It looks so dumb.

@Entity
@Table
@Data
public class User implements UserDetails {

    @Column(nullable = false)
    private String username;
    @Column(nullable = false)
    private String password;
    @Column(nullable = false)
    private String first_name;
    @Column(nullable = false)
    private String last_name;

}

Nikita Kalugin
  • 682
  • 2
  • 14
  • 37
  • 1
    is there a good reason why you use `Date` and not `LocalDate`? and regarding the `@Column(nullable=false)`, this might help you https://stackoverflow.com/questions/7439504/confusion-notnull-vs-columnnullable-false – Claudiu Guja Mar 11 '19 at 12:48
  • Actually no, i can use `LocalDate` will it helps me? – Nikita Kalugin Mar 11 '19 at 12:59
  • @ClaudiuGuja Thanks, `LocalDate` works fine! Can you add an answer to let me accept this, please? – Nikita Kalugin Mar 11 '19 at 13:30

3 Answers3

2

Instead of Date, it would be better to use LocalDate from Java8. This way, no time data should be stored in the database.

And regarding @Column(nullable=false), this might be of help: Confusion: @NotNull vs @Column(nullable = false)

Claudiu Guja
  • 290
  • 1
  • 3
  • 12
0

Properties are nullable by default in JPA, except primitive types. No need to add @Column(nullable = true) for String type

//default = nullable
@Column
private String prop;
Mebin Joe
  • 2,172
  • 4
  • 16
  • 22
0

I propose to use pattern parameter instead of iso parameter : @DateTimeFormat(pattern = "dd/MM/yyyy")