jack toubit
jack toubit

Reputation: 13

save data to the database using spring boot

when I post the data using the postman, the server replies with error code 500. the NetBeans terminal show:(java.sql.SQLIntegrityConstraintViolationException: Column 'email' cannot be null)

bellow my entityclass:

@Entity(name="user")
public class UserEntity  {

@Id
@GeneratedValue
 private long id;

@Column(nullable = true)
private String userId;

@Column(nullable = true)
private String FirstName;

@Column(nullable = true)
private String LastName;

@Column(nullable = true)
private String Email;

@Column(nullable = true)
private String Password;

@Column(nullable = true)
private String encryptedPassword;

@Column()
private String emailVerificationToken;

@Column()
private Boolean emailVerificationStatus=false;

public long getId() {
    return id;
}

public void setId(long id) {
    this.id = id;
}

public String getUserId() {
    return userId;
}

public void setUserId(String 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 String getEmail() {
    return Email;
}

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

public String getPassword() {
    return Password;
}

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

public String getEncryptedPassword() {
    return encryptedPassword;
}

public void setEncryptedPassword(String encryptedPassword) {
    this.encryptedPassword = encryptedPassword;
}

public String getEmailVerificationToken() {
    return emailVerificationToken;
}

public void setEmailVerificationToken(String emailVerificationToken) {
    this.emailVerificationToken = emailVerificationToken;
}

public Boolean getEmailVerificationStatus() {
    return emailVerificationStatus;
}

public void setEmailVerificationStatus(Boolean emailVerificationStatus) {
    this.emailVerificationStatus = emailVerificationStatus;
}

}

bellow is my service implementation class: /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.example.mobile.demo.impl;

import com.example.mobile.demo.DTo.UserDto;
import com.example.mobile.demo.Entity.UserEntity;
import com.example.mobile.demo.repository.UserRepository;
import com.example.mobile.demo.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
*
* @author iphone
*/
@Service
public class UserserviceImpl implements UserService{

@Autowired
UserRepository userRepository;//it is in  the data layer so we need the repository to save in the database

@Override
public UserDto createuser(UserDto user) {

    UserEntity userentity=new UserEntity();
    BeanUtils.copyProperties(user, userentity);
    System.out.println("the properties has been copied to the entity");
    userentity.setEncryptedPassword("test");
    userentity.setUserId("testID"); 
    System.out.println("encryptef passwird and user id has been set");


    UserEntity stotedValue=userRepository.save(userentity);




    UserDto returnValue=new UserDto();
    BeanUtils.copyProperties(stotedValue, returnValue);






    return returnValue;
}

}

my model class:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.example.mobile.demo.modoel;

 /**
 *
 * @author iphone
 */
public class Model {

 private String FirstName;
 private String LastName;
 private String Email;
 private String Password;

 public String getFirstName() {
    return FirstName;
 }

 public void setFistName(String FirstName) {
    this.FirstName = FirstName;
 }

 public String getLastName() {
    return LastName;
 }

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

 public String getEmail() {
    return Email;
 }

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

 public String getPassword() {
    return Password;
 }

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

}

bellow is what Iam sending in the post request in the postman:

{
"FirstName":"jack",
"LastName":"testjack",
"Password":"124",
"Email":"[email protected]"
}

Upvotes: 1

Views: 2688

Answers (1)

i.bondarenko
i.bondarenko

Reputation: 3572

The issue is in json to java Model mapping.

You need to rename your Model.java properties in this way:

 Email -> email 
 FirstName -> firstName

Or add @JsonProperty("name"):

@JsonProperty("email")
private String Email;

Don't forget json changes, if you choose properties renaming:

{
   "firstName":"jack",
   "lastName":"testjack",
   "password":"124",
   "email":"[email protected]"
}

Upvotes: 2

Related Questions