"UserEntity" @Data @Accessors(chain = true) @Entity @Table(name = "user") public class UserEntity { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @OneToOne(mappedBy = "user", fetch = FetchType.LAZY) @JsonManagedReference private UserArchiveEntity userArchive; } "UserArchiveEntity" @Data @Accessors(chain = true) @Entity @Table(name = "user_archive") public class UserArchiveEntity { @Id @Column(name = "user_id") private Long userId = 0L; @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id", insertable = false, updatable = false) // 从属关系:从;一定要加这个,避免json序列化循环引用导致报错 @JsonBackReference private UserEntity user; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "nation_id", insertable = false, updatable = false) private NationEntity nation; @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "industry_id", insertable = false, updatable = false) private IndustryEntity industry; } "UserRepository" @Repository public interface UserRepository extends JpaRepository, JpaSpecificationExecutor { @EntityGraph(attributePaths = {"userArchive", "userArchive.nation", "userArchive.industry"}) @NotNull @Override Page findAll(@NotNull Specification specification, @NotNull Pageable pageable); 我的查询如下: public List recommend(int size) throws Exception { UserEntity userEntity = UserContext.get(); Optional optionalUserArchiveEntity = this.userArchiveRepository.findByUserId(userEntity.getId()); if (optionalUserArchiveEntity.isEmpty()) { throw new Exception("用户档案记录不存在"); } UserArchiveEntity userArchiveEntity = optionalUserArchiveEntity.get(); Integer gender = userArchiveEntity.getGender(); Integer judgeGender = gender == UserArchiveEntity.GENDER_MALE ? UserArchiveEntity.GENDER_FEMALE : UserArchiveEntity.GENDER_MALE; Specification specification = (root, query, criteriaBuilder) -> { Predicate predicate = criteriaBuilder.conjunction(); Join userArchiveEntityJoin = root.join("userArchive"); return predicate; }; Sort sort = Sort.by(Sort.Direction.DESC, "infoCompletedRatio"); Pageable pageable = PageRequest.of(0, size, sort); return this.userRepository.findAll(specification, pageable).getContent(); } 出现的问题: 1. 上述代码生成的sql针对 "userArchive" 会 "left join" 两次!即 "user left join userarchive on xxx left join userarchive on xxx left join nation on xx on left join industry on xx",该如何避免重复 "join" 2. "findAll" 方法针对不同的业务场景关联的实体要求不一样;但是 spring jpa 没法声明方法签名一致的方法,也没法随意声明不同名称的方法(需要符合 spring jpa 的规范名称才会被正确解析),且由于要支持动态条件查询,必须要有 specification 这种 criterial 标准api 。该如何针对不同业务场景实现不同的模型关联? 无
spring jpa 针对复合主键如何通过 @Query 查询? @Query("select count(id) from UserRecommendedEntity where id.userId = :userId and isLike = :isLike") int countByUserIdAndIsLike(Long userId, int isLike); 上面代码ide会提示无法解析的字段 "id.userId"。我的 UserRecommendedEntity 代码如下: @Data @Accessors(chain = true) @Entity @Table(name = "user_recommended") public class UserRecommendedEntity { @EmbeddedId private UnionId id; @Column(name = "is_like") private Integer isLike; @Column(name = "created_at") private String createdAt; @Data public static class UnionId implements Serializable { @Column(name = "user_id") private Long userId; @Column(name = "recommended_user_id") private Long recommendedUserId; } } 无