【发布时间】:2020-02-02 11:59:47
【问题描述】:
我有一个 Comment 类,其 user 属性定义为:
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
@NonNull
private User user;
我有一个CommentRepository:
public interface CommentRepository extends CrudRepository<Comment, Integer> {
List<Comment> findByUserId(Integer userId);
}
我想通过他的 id 查询特定用户的 cmets。
我正在这样做:
commentRepository.findByUserId(userId);
一切正常,除了查询看起来像:
select
comment0_."id" as id1_1_,
comment0_."text" as url2_1_,
comment0_."user_id" as user_id3_1_
from
"comments" comment0_
left outer join
"users" user1_
on comment0_."user_id"=user1_."id"
where
user1_."id"=?
我想避免这种连接,因为我可以通过comments 表中的user_id 列直接查询。
我不想使用@Query注解,我认为应该有更聪明的方法。
【问题讨论】:
标签: java spring spring-boot jpa