Reputation: 112
I have two entities (Job and Location) that are connected through a many-to-many relationship. Recently we implemented soft delete logic on the Location entity and then added a filter on the job Mapping like this:
HasManyToMany(x => x.Locations)
.Table("JobLocation")
.ParentKeyColumn("JobId")
.ChildKeyColumn("LocationId")
.ApplyChildFilter<ExcludeDeletedFilter>("IsDeleted = :condition")
.Cascade.None();
The query for the many-to-many relationship looks like this
select ...
from Job job
left outer join JobLocation jl
on jl.JobId = job.Id
left outer join Location loc
on loc.Id = jl.LocationId and IsDeleted = 0
The problem is that now, when fetching a Job that has some deleted locations, the Locations collection on the job entity contains a null entry for each deleted Location.
What is the best way to handle this soft delete records when they are fetched through a many-to-many relationship. (for one to many, this filter does a great job)
What other alternatives should I consider to do this?
Upvotes: 1
Views: 436
Reputation: 5629
Items in a list are associated with an index. When persisted using a list-style mapping, these indices will be preserved (lists normally don't suddenly rearrange themselves).
If you filter some items, to make the visible items have stable positions, it follows that there must be null items for the hidden elements.
Consider using a different mapping, such as a set, bag or map.
Upvotes: 1