title: Spring JPA Repository - Separate Insert and Update Method
body: <p>How to create custom method from Spring JPA Repository that <strong>separates insert and update functionality</strong>? Let's say <strong>create method for insert</strong>, and <strong>update method for update</strong>.</p>
  
  <p>Extra question:
  <strong>Why Spring JPA Repository doesn't separate those methods by design?</strong></p>
  
  <p>My current implementation is <strong>create validation at Service layer</strong>.</p>
  
  <p><strong>My Repository</strong></p>
  
  <pre><code>public interface UserRepository extends CrudRepository&lt;User, Integer&gt; {}
  </code></pre>
  
  <p><strong>My Service</strong></p>
  
  <pre><code>@Service
  public class UserService {
      public void addUser(User user) {
          if (!userRepository.exists(user.getId())) {
              userRepository.save(user);
          }
      }
  
      public void updateUser(int id, User user) {
          if (userRepository.exists(user.getId())) {
              userRepository.save(user);
          }
      }
  }
  </code></pre>