In Blaze Persistence, is there a way to use an optional parameter in a subquery?
In my current use case, let's say I have topics, users, and a third table called topic_last_seen
to record the date at which each user has last read each topic. I would like to create an entity view for the Topic
entity which also maps that date for each topic with the current user given as an "optional parameter". This is roughly what I've been trying:
@EntityView(Topic.class)
public interface TopicView
{
@IdMapping
Long getId();
String getSummary();
@MappingParameter("currentUserId")
Long getCurrentUserId();
@MappingSubquery(LastSeenDateSubqueryProvider.class)
Date getLastSeenDate();
class LastSeenDateSubqueryProvider implements SubqueryProvider
{
@Override
public <T> T createSubquery(SubqueryInitiator<T> subqueryBuilder)
{
return subqueryBuilder.from(TopicLastSeen.class, "lastSeen")
.select("dateSeen")
.where("lastSeen.user.id").eqExpression("EMBEDDING_VIEW(currentUserId)")
.where("lastSeen.topic.id").eqExpression("EMBEDDING_VIEW(id)")
.end();
}
}
}
Unfortunately, this results in an exception:
java.lang.IllegalArgumentException: Attribute 'currentUserId' not found on type 'Topic'
Is there any way to use an optional parameter in a subquery like this at all? Or is there a different way to map this information? Changing the DB schema is not an option at this point.