JQPL自定义查询,无法返回@ManyToMany中对象列表。

huangapple go评论71阅读模式
英文:

JQPL custom query, having trouble returning list of objects in @ManyToMany

问题

以下是翻译好的内容:

我有两个模型和一个 @ManyToMany 表。一个用户表和一个图片表。用户表中有一个特定用户关注的其他用户列表。这些用户与每张图片都有 @OneToMany 关联。

用户模型:

@OneToMany(mappedBy="user", cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List<Picture> pics;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
	name="follows",
	joinColumns = @JoinColumn(name="user_id"),
	inverseJoinColumns = @JoinColumn(name = "following_id")	
)
private List<User> following;

图片模型:

private String image_url;
private String description;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="user_id")
private User user;

获取与所有用户关联的所有图片的 SQL 查询语句如下:

SELECT * FROM `pictures` picture
WHERE `user_id` = '1' OR `user_id` IN (
    SELECT `following_id` FROM `follows` WHERE `user_id`= 1
)

但是 follows 表不是一个独立的实体,我正在努力想象如何将这个 SQL 转换为自定义查询的 HQL。我仍然无法弄清楚,我得到的最接近的是:

@Query(value="SELECT p FROM Picture p WHERE p.user = :user OR p.user IN(SELECT u FROM User u JOIN u.following f WHERE f = :user)")
List<Picture> findByPictureFollower(@Param("user") User user);

我不确定我是否走对了方向。我想检索与用户模型中的 Following 列表数组中的每个用户对象关联的每个图片对象。欢迎提出您的想法。

英文:

I have 2 Models, and @ManyToMany table. A User table, and a Picture table. The user table has a list of other users that specific one follows. The users have a @OneToMany association with each Picture.

User

@OneToMany(mappedBy=&quot;user&quot;, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
private List&lt;Picture&gt; pics;

@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
	name=&quot;follows&quot;,
	joinColumns = @JoinColumn(name=&quot;user_id&quot;),
	inverseJoinColumns = @JoinColumn(name = &quot;following_id&quot;)	
)
private List&lt;User&gt; following;

Picture

private String image_url;
private String description;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name=&quot;user_id&quot;)
private User user;

The SQL Query to get all pictures associated with a all users (in User.following) is:

SELECT * FROM `pictures` picture
WHERE `user_id` = &#39;1&#39; OR `user_id` IN (
    SELECT `following_id` FROM `follows` WHERE `user_id`= 1
)

But the follows table is not it's own Entity, I'm racking my brain on how to translate that SQL into HQL for the custom query. I'm still not able to figure it out and the closest I've gotten

@Query(value=&quot;SELECT p FROM Picture p WHERE p.user = :user OR p.user IN(SELECT p.followers FROM p.followers WHERE user = :user)&quot;)
List&lt;Picture&gt; findByPictureFollower(@Param(&quot;user&quot;) User user);

I'm not sure if I'm going about this the right way. I want to retrieve every picture object that's associated with each user object in the Following List Array on the User model. Any thoughts would be appreciated.

答案1

得分: 1

以下对您是否有效?

@Query(value="SELECT p FROM Picture p WHERE p.user = :user OR p.user IN(SELECT f FROM User u JOIN p.followers f WHERE u = :user)")
List<Picture> findByPictureFollower(@Param("user") User user);
英文:

Does the following work for you?

@Query(value=&quot;SELECT p FROM Picture p WHERE p.user = :user OR p.user IN(SELECT f FROM User u JOIN p.followers f WHERE u = :user)&quot;)
List&lt;Picture&gt; findByPictureFollower(@Param(&quot;user&quot;) User user);

huangapple
  • 本文由 发表于 2020年10月24日 04:08:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/64506682.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定