获取用户关注的个人资料的帖子
Fetching the posts of the profiles the user follows
在我的数据库模式中,我有两个 tables
- UserPublisher table :它包含当前登录用户遵循的用户配置文件数量的信息。一个用户关注多个配置文件
- Post table : 它包含 post 用户 post.
现在我想获取当前用户关注的 post 用户配置文件。
我正在使用 hibernate 进行对象关系映射。
目前我正在获取用户关注的个人资料,然后 运行 在 POST table 上查询用户个人资料列表。
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
最好的方法是什么?在这种情况下我必须触发两个数据库查询,我希望这可以通过一个查询或其他方式实现
怎么样
SELECT Post.* FROM Post
INNER JOIN UserPublisher ON Post.user_id = UserPublisher.user_publisher_id
WHERE UserPublisher.user_id = {currentSessionId}
在我的数据库模式中,我有两个 tables
- UserPublisher table :它包含当前登录用户遵循的用户配置文件数量的信息。一个用户关注多个配置文件
- Post table : 它包含 post 用户 post.
现在我想获取当前用户关注的 post 用户配置文件。
我正在使用 hibernate 进行对象关系映射。
目前我正在获取用户关注的个人资料,然后 运行 在 POST table 上查询用户个人资料列表。
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
最好的方法是什么?在这种情况下我必须触发两个数据库查询,我希望这可以通过一个查询或其他方式实现
怎么样
SELECT Post.* FROM Post
INNER JOIN UserPublisher ON Post.user_id = UserPublisher.user_publisher_id
WHERE UserPublisher.user_id = {currentSessionId}