Nikita Kalugin
Nikita Kalugin

Reputation: 742

Spring Boot & Hibernate. Date auto-create

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;

}

Upvotes: 0

Views: 2957

Answers (3)

Claudiu Guja
Claudiu Guja

Reputation: 350

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)

Upvotes: 2

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

Upvotes: 0

Mebin Joe
Mebin Joe

Reputation: 2209

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;

Upvotes: 0

Related Questions