I'm using Spring with Spring Data Redis.
Our requirements entail that we need to be able to allow dynamic reference fields to be added to an entry in the DB and queried based off some base model. We are using Redis as a database, not as a cache in this particular scenario.
Example base model:
@RedisHash("BaseModel")@NoArgsConstructor@Getter@Setterpublic class BaseModel { @Id private String id; private Map<String, Object> references;}
The idea is to allow for dynamic objects based off a base model without having to go and create more subclasses to accommodate for new reference fields so that something like this is possible:
["123": {"id": "123","refA": "abc" },"321": {"id": "321","refB": "cba" },"456": {"id": "456","refB": "cba","refC": "xyz" }]
At the moment, the DB structure that is created based on the Java class above is as follows:
["123": {"id": "123","references": {"refA": "abc" } },"321": {"id": "321","references": {"refB": "cba" } },"456": {"id": "456","references": {"refB": "cba","refC": "xyz" } }]
It would be preferable to nest these dynamic reference fields directly in the object and not inside another object (references
in the example).
What we need is something similar to this question, but instead of using MongoDB, it needs to be for Redis.
I have tried some rudimentary tests with the use of Spring Repository (e.g. CrudRepository
) but I am getting stuck on how to set up the queries. It seems that Mongo has the MongoTemplate
class which has support for dynamic field querying, but RedisTemplate
seems to work differently entirely.
It also seems that Spring Data Redis doesn't support the @Query
annotation as with Mongo and JPA, so that is also a bit of a blocker as to how to proceed.
Any ideas, nudges, links, etc. would be appreciated in relation to how to set the model up as well as how to query it using Redis as database (not as cache).