How to implement a convenient filter for the parameters when working with a database? There are certain parameters, for example color, length, weight that come with a UI. In the controller, all this is handled somehow. And already on the repository layer, according to the parameters, you need to extract the necessary objects. For example, first, let's say a weight of 20kg is chosen, and then another + parameter length is 2m, so already two parameters should participate in the request. And so to +5 ... 10 parameters. Offer all Hibernate, Jpa, Spring jdbc, data-jpa. Maybe there is some simple way to do this?
1 answer
Option 1 . Use Apache Lucene or Sphinx. Apache Lucene is written in Java, so it is easy to integrate it into a project in Spring. Sphinx runs as a separate process with communication over TCP (or through a pipe), so you need to write your connection manager or use the API port on JAVA, which was last updated 10 years ago. But Sphinx has fewer resources, and Lucene needs from 2 GB of RAM for normal operation.
Option 2 . You create 3 tables:
item- item,property- property,item_property- property correspondence to item.
For example, will be:
item: --------- id | name --------- 1 | IPhone property: --------- id | name --------- 1 | Вес 2 | Цвет item_property: ----------------------------- item_id | property_id | value ----------------------------- 1 | 1 | 5 1 | 2 | белый Then you will choose through INNER JOIN and UNION ALL . That is, for queries Hibernate will not help you - you will need to use native queries.
- I did a little differently. in two tables. as such: the item table with id and name fields, and property with item_id and id fields (the value itself, red for example). That is, each property has its own table. It turns out that for each property, that is, a table, do you need to create your own class-entity? - cori
- @cori, in your case, yes. If there are only 2 tables, then it will be hard for you to do a search, because in your case it is difficult to make a difference in the name of a particular parameter. If you have a table for each property, then when you add a new property, you will have to climb into the database and code and create classes. I recommend using option 2, as this is a standard scheme. - Victor Khovanskiy