无法将数据从Spring引导设置器方法发送到数据库

pjngdqdw  于 2022-10-15  发布在  Spring
关注(0)|答案(2)|浏览(154)

我无法使用Entity类中的setter方法手动将数据发送到数据库。代码在Spring Boot中运行成功,但手动输入的数据没有反映在数据库中,当我使用 Postman 发送数据时,代码运行正常
下面是我的实体类

@Entity
@Table(name="users")
@JsonIgnoreProperties({"hibernateLazyInitializer"})
public class Users {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int userid;

    @NotNull(message="firstname cannot be null")
    @Column(name= "firstName")
    private String firstName;

    @NotNull(message="lastname cannot be null")
    @Column(name= "lastName")
    private String lastName;

    @Column(name= "contact", unique = true)
    private Long contact;

    @NotNull(message="email cannot be null")
    @Column(name= "email", unique = true)
    private String email;

    @NotNull(message="isActive cannot be null")
    @Column(name= "isActive")
    private boolean isActive;

    @NotNull(message="ggid cannot be null")
    @Column(name="ggid", unique = true)
    private int ggid;

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

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

    @NotNull(message="password cannot be null")
    @Column(name="password")
    private String password;

    @NotNull(message="userName cannot be null")
    @Column(name="userName")
    private String userName;

    @NotNull(message="supervisor cannot be null")
    private String supervisor;

    public Users() {
        super();
    }

    public Users(int userid, @NotNull(message = "firstname cannot be null") String firstName,
            @NotNull(message = "lastname cannot be null") String lastName, Long contact,
            @NotNull(message = "email cannot be null") String email,
            @NotNull(message = "isActive cannot be null") boolean isActive,
            @NotNull(message = "ggid cannot be null") int ggid, String role, String gender,
            @NotNull(message = "password cannot be null") String password,
            @NotNull(message = "userName cannot be null") String userName,
            @NotNull(message = "supervisor cannot be null") String supervisor) {
        super();
        this.userid = userid;
        this.firstName = firstName;
        this.lastName = lastName;
        this.contact = contact;
        this.email = email;
        this.isActive = isActive;
        this.ggid = ggid;
        this.role = role;
        this.gender = gender;
        this.password = password;
        this.userName = userName;
        this.supervisor = supervisor;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Long getContact() {
        return contact;
    }

    public void setContact(Long contact) {
        this.contact = contact;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public int getGgid() {
        return ggid;
    }

    public void setGgid(int ggid) {
        this.ggid = ggid;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSupervisor() {
        return supervisor;
    }

    public void setSupervisor(String supervisor) {
        this.supervisor = supervisor;
    }

}

业务类中的数据发送方法为

public void Addadmin() {
        Users admin =new Users();
        admin.setUserid(1);
        admin.setFirstName("abc");
        admin.setLastName("def");
        admin.setContact((long) 123456);
        admin.setEmail("admin@gmail.com");
        admin.setActive(true);
        admin.setGgid(1234);
        admin.setRole("admin");
        admin.setGender("male");
        admin.setPassword("password");
        admin.setUserName("admin");
        admin.setSupervisor("X");
        repository.save(admin);

    }

谢谢你们

prdp8dxp

prdp8dxp1#

您是否尝试在不填充userid字段的情况下保存?它用@GeneratedValue(strategy = GenerationType.IDENTITY)注解,在创建过程中应该为空。

hmtdttj4

hmtdttj42#

试试这个:

@Entity
@Table(name="users")
@JsonIgnoreProperties({"hibernateLazyInitializer"})
public class Users {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int userid;

@NotNull(message="firstname cannot be null")
@Column(name= "firstName")
private String firstName;

@NotNull(message="lastname cannot be null")
@Column(name= "lastName")
private String lastName;

@Column(name= "contact", unique = true)
private Long contact;

@NotNull(message="email cannot be null")
@Column(name= "email", unique = true)
private String email;

@NotNull(message="isActive cannot be null")
@Column(name= "isActive")
private boolean isActive;

@NotNull(message="ggid cannot be null")
@Column(name="ggid", unique = true)
private int ggid;

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

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

@NotNull(message="password cannot be null")
@Column(name="password")
private String password;

@NotNull(message="userName cannot be null")
@Column(name="userName")
private String userName;

@NotNull(message="supervisor cannot be null")
private String supervisor;

public Users() {
    super();
}

public Users(int userid, @NotNull(message = "firstname cannot be null")    String firstName,
        @NotNull(message = "lastname cannot be null") String lastName, Long contact,
        @NotNull(message = "email cannot be null") String email,
        @NotNull(message = "isActive cannot be null") boolean isActive,
        @NotNull(message = "ggid cannot be null") int ggid, String role, String gender,
        @NotNull(message = "password cannot be null") String password,
        @NotNull(message = "userName cannot be null") String userName,
        @NotNull(message = "supervisor cannot be null") String supervisor)      {
        super();
        this.userid = userid;
        this.firstName = firstName;
        this.lastName = lastName;
        this.contact = contact;
        this.email = email;
        this.isActive = isActive;
        this.ggid = ggid;
        this.role = role;
        this.gender = gender;
        this.password = password;
        this.userName = userName;
        this.supervisor = supervisor;
    }

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Long getContact() {
        return contact;
    }

    public void setContact(Long contact) {
        this.contact = contact;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean isActive) {
        this.isActive = isActive;
    }

    public int getGgid() {
        return ggid;
    }

    public void setGgid(int ggid) {
        this.ggid = ggid;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
    this.role = role;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getSupervisor() {
        return supervisor;
    }

    public void setSupervisor(String supervisor) {
        this.supervisor = supervisor;
    }
}

public void Addadmin() {
    Users admin =new Users();
    admin.setFirstName("abc");
    admin.setLastName("def");
    admin.setContact((long) 123456);
    admin.setEmail("admin@gmail.com");
    admin.setActive(true);
    admin.setGgid(1234);
    admin.setRole("admin");
    admin.setGender("male");
    admin.setPassword("password");
    admin.setUserName("admin");
    admin.setSupervisor("X");
    repository.save(admin);

}

相关问题