0

I have Entity called MyEntity, one of the fields is List ids; I'm storing it in database as separated coma values, like 1,2,3 thanks to

@Convert(converter = StringListConverter.class)
private List<String> ids = new ArrayList<>();

I want to retrieve MyEntity from database specific id in mentioned list. Let's say number 1. I did something like this below. Just for testing.

List<MyEntity> al = trainingPlanRepository.findAll();
List<MyEntity> as = al.stream().filter(me -> me.getIds().contains("1")).collect(Collectors.toList());

I would prefer to create custom query to get that but because of the conversion I can not handle it. (miss match type). When I'm using db tool to handle my tables, below query works fine to achieve that, but it won't work as a custom jpa @Query. select * from my_entity where ids like '%1%';

MichalWds
  • 13
  • 7
  • 1
    Please look at this thread it might help https://stackoverflow.com/questions/25362540/like-query-in-spring-jparepository – Vikram Feb 13 '23 at 02:53
  • that would work only without annotation @Convert. That's the problem. How can I query that if my List is saved in db as varchar String elements – MichalWds Feb 13 '23 at 09:51
  • The List is for your application and doesn't represent the data as it is in the database anyway. So if you want to be able to treat 'ids' as a string, map it like one. You can handle the conversion to/from String->List yourself in the application using your converter in an accessor method on the String property set for JPA to use, while your application only ever deal with the data as a List property. PS like using '1' is going to pick up 100, 51 etc. You'll want to use more strict regex patterns that match your delimiters if you need to look for specific values – Chris Feb 13 '23 at 16:16

0 Answers0