I know that this can be easily solved with an HQL query, however I prefered to simply have Hibernate handle this with a few OneToMany properties 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, there are two foreign keys that both point to the Team table on the Game table. Clearly if there were only one foreign key then it would represent a true One-To-Many relationship but in reality what I want is two bi-directional One-To-Many properies for the same entity child type.
I don't believe using the @Where annotation will work as it requires a constant, and @JoinColumn is not allowed here. If it is not possible then that is okay, I just wanted to here it from somebody else.
I bet you don't really understand the use of mappedBy.
You may refer to my other answer in https://stackoverflow.com/a/13812047/395202
In short, mappedBy is the property name in the "opposite side" of a bi-directional relationships.
For you case, it probably look something like:
class TeamPlay {
@ManyToOne
Team homeTeam;
@ManyToOne
Team awayTeam;
}
class Team {
@OneToMany(mappedBy="homeTeam")
Set<TeamPlay> homeTeamPlays;
@OneToMany(mappedBy="awayTeam")
Set<TeamPlay> awayTeamPlays;
}
There is nothing wrong with your code. I've tested it with @ManyToOne on TeamPlay class and it works fine. Creates a join column on TeamPlay table as expected. Nothing unusual
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.