【发布时间】:2017-10-06 10:03:20
【问题描述】:
我正在尝试将 Hibernate 与 Spring Boot 一起使用。我也是使用 Hibernate 的新手。
我想在userUid 给定后加入两个表。
我的表如上。
StoreBranch 表
storeBranchUid |分行名 | ...
StoreBranchFavorite
storeSectionFavoriteUid | storeBranchUid |用户名
当给定userUid时,
我要实现这个SQL,
SELECT ...
FROM StoreBranch
JOIN StoreBranchFavorite
ON StoreBranch.storeBranchUid = StoreBranchFavorite.storeBranchUid
AND userUid = :userUid
我阅读了查询查找策略,但我认为没有办法实现这一点。
我应该使用命名查询还是其他方式?
public interface StoreBranchDao extends CrudRepository<StoreBranch, Long> {
List<StoreBranch> findBystoreBranchUid(int storeBranchUid);
// I want to find user's favorite branch when userUid has given.
List<StoreBranch> findByUserUid(Long userUid);
}
public class StoreBranch {
@Id
private int storeBranchUid;
@NotNull
private String branchName;
private String breakfastOpen;
private String breakfastClose;
private String lunchOpen;
private String lunchClose;
private String dinnerOpen;
private String dinnerClose;
// join the table when the userUid has given
@OneToMany
@JoinColumn(name="storeBranchUid", referencedColumnName="storeBranchUid")
private StoreBranchFavorite favorite;
}
public class StoreBranchFavorite {
@Id
private int storeSectionFavoriteUid;
@NotNull
private long userUid;
}
【问题讨论】:
-
您确实需要学习 Hibernate/JPA 的基础知识。阅读官方手册,了解关联和 JPQL 查询。您不应该存储其他实体的 ID。相反,您应该定义它们之间的关联。
标签: hibernate spring-boot orm