获取用户关注的个人资料的帖子

Posted

技术标签:

【中文标题】获取用户关注的个人资料的帖子【英文标题】:Fetching the posts of the profiles the user follows 【发布时间】:2015-10-26 15:10:00 【问题描述】:

在我的数据库架构中,我有两个表

    UserPublisher 表:它包含当前登录用户遵循的用户配置文件数量的信息。一个用户关注多个个人资料

    帖子表:它包含用户发布的帖子。

现在我想获取当前用户关注的用户个人资料的帖子。

我正在使用休眠进行对象关系映射。

目前我正在获取用户关注的配置文件,然后在 POST 表上运行查询以获取用户配置文件列表。

Integer userId=loggedInUser.getUserId();
Query query= sessionFactory.getCurrentSession().createQuery("from UserPublishers u where u.userId=:userId");
query.setParameter("userId", userId);
List<UserPublishers> userPublisherList = query.list();
//Now fetch the posts for users in this list from the POST table

这是最好的方法吗?在这种情况下,我确实必须触发两个数据库查询,我希望这可以通过一个查询或其他方式实现

【问题讨论】:

【参考方案1】:

类似的东西呢

SELECT Post.* FROM Post
INNER JOIN UserPublisher ON Post.user_id = UserPublisher.user_publisher_id
WHERE UserPublisher.user_id = currentSessionId

【讨论】:

以上是关于获取用户关注的个人资料的帖子的主要内容,如果未能解决你的问题,请参考以下文章