title: Spring Boot Jpa Delete all records except last 50
body: <p>im building a chat module for my site and I want to only show the users 50 last chats and when I add more than 50 delete the first chat and so on.</p>
  
  <p>I made it work with the SpringScheduler. I check every 5 mins and if the database has more than 50 records I delete the all except last 50. is there a better way to do this?</p>
  
  <p><em>EDIT</em></p>
  
  <p>im using 10.3.16-MariaDB (SQL) and Spring boot 2.2.6.</p>
  
  <p>I tried to add Triggers like this <a href="https://stackoverflow.com/questions/14281510/limiting-mysql-table-to-a-sertain-size-and-automatically-deleting-oldest-entries">Answer</a> but I'm getting this exception: </p>
  
  <pre><code>java.sql.SQLException: Can't update table 'chats' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
  </code></pre>