Java – Hibernate: two onetomany attributes mapped by the same entity with different columns
I know this can be easily solved through HQL query, but I prefer hibernate to handle some onetomany attributes for me
Let me demonstrate what I want my domain model to look like in pseudo code:
Game Long GameID Team HomeTeam Team AwayTeam @OneToMany(mappedBy="team") Set<TeamPlay> HomeTeamPlays @OneToMany(mappedBy="team") Set<TeamPlay> AwayTeamPlays
The table structure is similar, with two foreign keys pointing to the team table on the game table Obviously, if there is only one foreign key, it will represent a real one to many relationship, but in fact, what I want is two bidirectional one to many features of the same entity subtype
I don't believe the @ where annotation will work because it requires a constant and @ joincolumn is not allowed here If it's impossible, it doesn't matter. I just want to come from others
Solution
I bet you don't really understand the use of mappedby
You can https://stackoverflow.com/a/13812047/395202 Refer to my other answers in
In short, mappedby is the attribute name in the "opposite" of a two-way relationship
In your case, it may look like:
class TeamPlay { @ManyToOne Team homeTeam; @ManyToOne Team awayTeam; } class Team { @OneToMany(mappedBy="homeTeam") Set<TeamPlay> homeTeamPlays; @OneToMany(mappedBy="awayTeam") Set<TeamPlay> awayTeamPlays; }