Home > Back-end >  'default' is not working in @Column in Spring Boot
'default' is not working in @Column in Spring Boot

Time:10-10

Below is my entity class, I have added default contraint on my EnumType variable of class. But while the value in the db, it not taking that value and giving ContraintViolation. Entity

@Entity
@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "employee")
public class Employee{   
  @Id
  private String id;
  
  @Column(name = "name", nullable = false)
  private String name;
  
  @Column(name = "email", nullable = false)
  private String email;
  
  @Column(name = "language", nullable = false, columnDefinition = "VARCHAR(10) default 'ENGLISH'")
  @Enumerated(EnumType.STRING)
  private Language language;
  
}

Here, if I remove nullable from Language Enum, the language is saved as null. Why is it not taking default value as ENGLISH

CodePudding user response:

Since Language is of type ENUM, you can use:

@Column(name = "language", nullable = false, columnDefinition = "enum('ENGLISH' , 'CHINESE') default 'ENGLISH'")

CodePudding user response:

How about initializing your enum property with default value like below?

@Column(name = "language", nullable = false, columnDefinition = "VARCHAR(10) default 'ENGLISH'")
  @Enumerated(EnumType.STRING)
  private Language language = Language.ENGLISH;

Refer this answer for more details

  • Related