简体   繁体   English

无法为 Hibernate 分配默认的 user_role="ROLE_USER"

[英]Can't assign a default user_role="ROLE_USER" with Hibernate

I have a user model and I am triyng to set a default role="role_user" in my postgreSQL database when a user register.我有一个用户 model,我正在尝试在用户注册时在我的 postgreSQL 数据库中设置默认角色 =“role_user”。 My User model is:我的用户 model 是:

 @Entity
@Table(name = "users")
public class User {
@NotEmpty(message = "Campul nu poate fi lasat gol")
@Size(min = 4, max = 16,message = "Dimensiunea trebuie sa fie intre 4 si 16 caractere")
private String firstName;

@NotEmpty(message = "Campul nu poate fi lasat gol")
@Size(min = 4, max = 16,message = "Dimensiunea trebuie sa fie intre 4 si 16 caractere")
private String lastName;

@Id
@NotEmpty(message = "Campul nu poate fi gol")
@Email(message = "Email-ul trebuie sa fie valid")
private String email;

@NotEmpty(message = "Campul nu poate fi gol")
@Size(min = 4, max = 60, message = "Parola trebuie sa contina minim 4 caractere")
private String password;

private String role;

private boolean enabled = true;

public User() {};
public User(String firstName, String lastName, String email, String password,String role) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.password = password;
    this.role = role;

}

How can I solve the problem.我该如何解决这个问题。 I saw another posts on stackoverflow but I don't understand, I'm quite new in Spring我在 stackoverflow 上看到了另一个帖子,但我不明白,我是 Spring 中的新手

You can just initialize your field with the default value.您可以使用默认值初始化您的字段。

private String role = "role_user";

public User(String firstName, String lastName, String email, String password,String role) {
     this.firstName = firstName;
     this.lastName = lastName;
     this.email = email;
     this.password = password;
     if(role != null) { this.role = role; }; // if you need to keep the default value
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM