Is it possible to generate a list of only the distinct objects (from the child entity) using @Embedded and asscociatedBy = Junction() table?
I have several separated entities (EntityActivityLog, EntityComponent, EntityGroup) associated by the linking table (EntityLinkGroupToActivity).
Essentially each ActivityLog has multiple components, which in turn have multiple groups. (it goes on but for the purposes of this question I'll stop there)
At the moment Im working with the simplified data set as follows:
- EntityActivityLog (activityLogId, activityName) contains 2x unique activities
- EntityComponent (componentId, componentName) contains 5x unique components.
- EntityLinkGroupToActivity (activityId, componentId, groupId) contains 9x rows of data. The first activityLog has 3 unique components, and the second activityLog has 2 components.
[activityLog1, component1,group1]
[activityLog1, component1,group2]
[activityLog1, component1,group3]
[activityLog1, component2,group1]
[activityLog1, component4,group1]
[activityLog2, component1,group1] etc...
Using the following code I expected a pojo containing a single embedded activity object and a list of unique component entities. Each componentId would be associated with the activityLogId in the linking table.
data class ActivityLogWithComponentList (
@Embedded
val activityLog: EntityLogActivity,
@Relation(
parentColumn = "activityLogId",
entity = EntityComponent::class,
entityColumn = "componentId ",
associateBy = Junction(
EntityLinkGroupToActivity::class,
parentColumn = "activityLogId",
entityColumn = "componentId ",
)
)
val componentList: List<EntityComponent>
){}
However, in the result for the first activityLog there are 9x components.
POJO List Results:
component1, component1, component1, component2, component4...
I can understand the logic with the 'junction' of the linking table containing all the componentIds as does the component entity table. Thus each row of the junction is always fulfilled. However this is not the outcome Im looking for and haven’t seen an alternative to associateBy = junction().
Ideally I am aiming for only the distinct component items in the list: component1, component2, component4
Any suggestions would be appreciated on how to achieve this using this embedded approach.
Additional Notes:
- I dont believe I can break up the linking entity without introducing unwanted complexity.
- I have tried including 'Distinct' in the SQL query, but as the query only references the entity tables which are already distinct this has had no effect on the list result.