title: Spring Boot Data Rest JPA - Entity custom create (User)
body: <p>I am trying to learn Spring. I created a project with Spring Boot using the following tools:</p>
  
  <ul>
  <li>Spring Data JPA </li>
  <li>Spring Data REST</li>
  <li>Spring HATEOAS</li>
  <li>Spring Security</li>
  </ul>
  
  <p>I am trying to create a <code>User</code> entity. I want the user to have an encrypted  password (+ salt). </p>
  
  <p>When i do <code>POST</code> to <code>/api/users</code> i successfully create a new user. </p>
  
  <pre><code>{
  "firstname":"John",
  "lastname":"Doe",
  "email":"johndoe@example.com",
  "password":"12345678"
  }
  </code></pre>
  
  <p>But i have 2 problems:</p>
  
  <ul>
  <li>the password is saved in clear-text</li>
  <li>the salt is null</li>
  </ul>
  
  <blockquote>
  <pre><code>+----+---------------------+-----------+----------+----------+------+
  | id |        email        | firstname | lastname | password | salt |
  +----+---------------------+-----------+----------+----------+------+
  |  1 | johndoe@example.com | John      | Doe      | 12345678 | NULL |
  +----+---------------------+-----------+----------+----------+------+
  </code></pre>
  </blockquote>
  
  <p>The problem i think is that the default constructor is used and not the other one i have created. I am new to Spring and JPA so i must be missing something. Here is my code. </p>
  
  <p><strong>User.java</strong></p>
  
  <pre><code>@Entity
  @Table(name = "users")
  public class User{
  
      @Id
      @GeneratedValue
      private Long id;
  
      @Column(nullable = false)
      public String firstname;
  
      @Column(nullable = false)
      public String lastname;
  
      @Column(nullable = false, unique = true)
      public String email;
  
      @JsonIgnore
      @Column(nullable = false)
      public String password;
  
      @JsonIgnore
      @Column
      private String salt;
  
      public User() {}
  
      public User(String email, String firstname, String lastname, String password) {
          this.email = email;
          this.firstname = firstname;
          this.lastname = lastname;
          this.salt = UUID.randomUUID().toString();
          this.password = new BCryptPasswordEncoder().encode(password + this.salt);
      }
  
  
      @JsonIgnore
      public String getSalt() {
          return salt;
      }
  
      @JsonProperty
      public void setSalt(String salt) {
          this.salt = salt;
      }
  
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
      public String getFirstname() {
          return firstname;
      }
  
      public void setFirstname(String firstname) {
          this.firstname = firstname;
      }
  
      public Long getId() {
          return id;
      }
  
      public void setId(Long id) {
          this.id = id;
      }
  
      public String getLastname() {
          return lastname;
      }
  
      public void setLastname(String lastname) {
          this.lastname = lastname;
      }
  
      @JsonIgnore
      public String getPassword() {
          return password;
      }
  
      @JsonProperty
      public void setPassword(String password) {
          this.password = password;
      }
  
  }
  </code></pre>
  
  <p><strong>UserRepository.java</strong></p>
  
  <pre><code>public interface UserRepository extends JpaRepository&lt;User, Long&gt; {
  
      public User findByEmail(String email);
  
      public User findByEmailAndPassword(String email, String password);
  }
  </code></pre>
  
  <p><strong>Application.java</strong></p>
  
  <pre><code>@SpringBootApplication
  public class Application {
  
  
      public static void main(String[] args) {
          SpringApplication.run(Application .class, args);
      }
  
  }
  </code></pre>
  
  <p>Also if someone finds what i did wrong, i would like to point me where/how i should put the user login code (decryption).</p>
  
  <p>Thanks.</p>