Home > Software design >  Unable to send data to DB from spring boot setters methods
Unable to send data to DB from spring boot setters methods

Time:10-06

I unable to manually send data to the DB using setter methods in the entity class. The code is running successfully in spring boot but the manually typed data not reflecting in database and its working fine when I used to send data using postman

here is my entity class

@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;
    }



    
    
    
}

and data sending method in service class is

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

thank you guys

CodePudding user response:

Is there any error thrown? Can you share full project so i can recreate?

CodePudding user response:

Have you tried to save without userid field populated? It is annotated with @GeneratedValue(strategy = GenerationType.IDENTITY) it should be empty during creation.

  • Related