我想在我的存储库界面中构造一个find
查询,以选择由多对多关系映射的所有元素。
User.java:
@Entity
@Table(name = "user")
public class User {
@Id
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;
@Column(name = "username")
private String username;
@Column(name = "email")
private String email;
@Column(name = "passwordHash")
private String passwordHash;
@ManyToMany(mappedBy = "usersTracking")
private Set<Show> shows;
...
Show.java:
@Entity
@Table(name = "shows")
public class Show {
@Id
@Column(name = "id", columnDefinition = "BINARY(16)")
private UUID id;
@Column(name = "imdb_id", length = 9, nullable = false)
private String imdbID;
@Column(name = "title", length = 100, nullable = false)
private String title;
@Column(name = "poster_url", length = 255, nullable = true)
private String posterURL;
@Column(name = "rating", length = 3, nullable = true)
private double rating;
@Column(name = "genre", length = 100, nullable = true)
private String genre;
@ManyToMany
@JoinTable(name = "users_shows",
joinColumns = @JoinColumn(name = "show_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"))
private Set<User> usersTracking;
...
要使用查询构建器实现这一点(我认为它已被调用),我已经编写了一个存储库:
public interface ShowRepository extends CrudRepository<Show, UUID> {
List<Show> findByUser(User user);
}
但我希望能够从我的UserRepository而不是ShowRepository获取节目,所以我应该写这样的东西?
public interface UserRepository extends CrudRepository<User, UUID> {
List<Show> findShows();
//Or maybe?
List<Show> findShowsByUser(User user);
}
我是否正确理解整个查询构建正确?