I have been looking the forums and everywhere for a unidirectional manyToMany query. I find many examples but i can't really adjust them to my needs :(
I have 2 entity classes (Anime & User)
Anime {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "anime_id")
private Integer id;
//Other fields etc.
}
User {
@Id
@ValidUsername
@Column(name = "user_id")
private String username;
@ManyToMany()
@JoinTable(name = "users_animes",
joinColumns = @JoinColumn(name = "anime_id", referencedColumnName = "anime_id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "user_id"))
private final List<Anime> animes = new ArrayList<>();
}
Anime simply holds the data from the anime. User holds the username etc of the user and a list of anime that he subscribed to.
Now i'm trying to find a query that will let me get all the animes in that list. It's mapped in a table as "users_animes".
Would be a huge help since i'm fairly new to JPQL.
Thanks!
Here's a simple example. Let's assume that we have Country Entity, which can have multiple Citizens:
And Citizen, which can belong to multiple Countries:
It's unidirectional, just as you wanted it to be. Therefore, Citizen Entity is unaware of Country, so you can't get directly information to which Country a certain Citizen belongs. However, you can retrieve info about which Citizens belong to a certain Country.
Going further, you can populate your tables:
And finally: