1

I use Spring Data JPA and Hibernate trying to implement soft delete. All examples I seen are very simple where clauses like a boolean check. What I need is for example soft delete all dogs if their breed is in a list of banned breeds for example. I have a dynamic list:

List<String> bannedBreedsComesFromPropertiesFile = Arrays.asList("beagle", "terrier", "dalmation")

And repo:

public interface DogRepository extends CrudRepository<Dog, Integer> {

   findAllBy....
   findAllBy...
   findAllBy... 
   ....
} 

So the breed filter should apply to all above findAll methods.

And then I want to achieve a soft delete/Filter with these dog breeds from DB:

  @Entity
  @Where(clause="breed != Not in list bannedBreedsComesFromPropertiesFile")

  public class Dog{


  @Column(name="breed")
  private String breed;

 }

How can I achieve this? I cant find the correct syntax if it exists.

Spring
  • 11,333
  • 29
  • 116
  • 185
  • 1
    Soft delete would relate to setting some flag to indicate a record is deleted rather than physically deleting it. From the code you have posted it looks like you only want to filter the records by some criteria. These are 2 completely different things.Additionally, surely banned breeds should be in the database rather than in a properties file. – Alan Hay Jul 17 '19 at 11:37
  • @Alan Hay thanks . you are right do, you have a solution for filters? I see there is FilterDef annotation but cant see how I can send a dynamic list to that. To your second remark, banned breeds needs to be updated frequently, and for the moment properties file its good enough – Spring Jul 17 '19 at 11:44
  • @AlanHay Also you can see more details on my question here: https://stackoverflow.com/questions/57061546/spring-data-repo-general-criteria – Spring Jul 17 '19 at 11:46

0 Answers0