Overview#
This week I really had to think about the entity relationships in the database, more so in terms of understanding all of the different relationship types. Do I model this as many-to-many, many-to-one, or perhaps one-to-one? This took unbelievable brainpower, to the point where I had to call in support from Codex to make sure I was doing the right thing.
Users and Interactions#
I introduced UserInteraction as its own entity rather than storing likes or bookmarks directly on User or Content.
By making interaction its own entity, I could represent:
- which user made the interaction
- what content it belongs to
- what kind of reaction it was
- when it happened
@Entity
@Table(
name = "user_interactions",
uniqueConstraints = {
@UniqueConstraint(
name = "uk_user_interaction_user_content",
columnNames = {"user_id", "content_id"}
)
}
)
public class UserInteraction {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id", nullable = false)
private User user;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "content_id", nullable = false)
private Content content;
}Note: I’m using FetchType.LAZY here because it avoids loading related data unless it is actually needed.
Final Thoughts on the Third Week#
This week taught me that relationships are hard, and it’s even harder when your job is to fire the correct Cupid arrows at the right entities, because otherwise the result is wrong domain logic, and no one wants that.