title: How to put check if record already exist - spring boot
body: <p>I'm new in spring &amp; try to enhance my skill by creating little projects. In my new app I'd like to put uniqueness check on accountNumber, unfortunately I did not get success. I'd like to apply isPresent() method but it doesn't exist when I call it. Grateful if I get help.</p>
  <p>Following is my Service class where I apply logics, I also mention Rep. &amp; Ent. classes for your reference. I apply unique condition in 3rd line where find ***if ..... ***.</p>
  <p>I've found similar question but the solution in this stack doesn't match with the way I like to do, I'd like to use JPA not JPQL.</p>
  <p>AccountService</p>
  <pre><code>@Autowired
    private AccountRepository accountRepository;
  
  @Transactional
    public ResponseEntity&lt;Object&gt; addAccount(CurrentAccount currentAccount){
      **if(currentAccount.getAccountNumber().is)**
      accountRepository.save(currentAccount);
      return ResponseEntity.ok(&quot;Account Accepted&quot;);
  } 
  </code></pre>
  <p>CurrentAccount</p>
  <pre><code>public class CurrentAccount {
  @Id
  @GeneratedValue(strategy = GenerationType.AUTO)
  @Column(name = &quot;id&quot;)
  private Long id;
  @Column(name = &quot;account_number&quot;)
  private String accountNumber;
  @Column(name = &quot;account_balance&quot;)
  private Integer accountBalance;
  </code></pre>
  <p>AccountRepository</p>
  <pre><code>public interface AccountRepository extends JpaRepository&lt;CurrentAccount, Long&gt; {
      Optional&lt;CurrentAccount&gt; findByAccountNumber(String accountNumber);
  }
  </code></pre>