title: How do i check if a record already exists in table in springboot JPA?
body: <p>I have a table with 4 fields. And if i inserted a record that already exists i.e all field value matches with previous record in table. How do i return record only but not insert into database ?</p>
  <p>My model look like this:</p>
  <pre><code>@Entity
  public class QuestionDetails {
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private int id;
  
      private String department;
      private String year;
      private String academic_year;
      private String semester;
      private String type;
      private String subject;
      private int points;
      private int unit;
  
  // getter, setter
  </code></pre>
  <p>And Controller look this:</p>
  <pre><code>@Autowired
  public QuestionDetailsRepository qdRepository;
  
   @PostMapping(&quot;/api/questionDetails&quot;)
   public QuestionDetails addQuestion(@Valid @RequestBody QuestionDetails qDetails) {
  
  // here i want to first check if qDetails object is already present in table . 
  If present i want to return that existed record instead of inserting into table.
  
  
              QuestionDetails qd = qdRepository.save(qDetails); // save only new record
      
              return qd;
          }
  </code></pre>
  <p>Using postman i send data like this:</p>
  <pre><code>{
   &quot;department&quot; : &quot;IT&quot;,
   &quot;year&quot; : &quot;2020&quot;,
   &quot;academic_year&quot; : &quot;1st year&quot;,
   &quot;semester&quot; : &quot;first semester&quot;,
   &quot;type&quot; : &quot;objective&quot;,
   &quot;subject&quot; : &quot;JAVA&quot;,
   &quot;points&quot; : 10,
   &quot;unit&quot; : 5
  }
  </code></pre>
  <p>Here, i am sending data that is already present in table. So, i want to check if this record already exist? If doesn't exist insert into table otherwise return that existed record.</p>
  <p>How do i achieve that using springboot Jpa hibernate?</p>