【问题标题】:Spring Data JPA derived query returns 0 records where as @Query fetches correct recordSpring Data JPA 派生查询返回 0 条记录,其中 @Query 获取正确的记录
【发布时间】:2021-09-30 12:40:47
【问题描述】:

我的 JPA 派生方法 findByBatchIdAndInstitute(Long id, Institute inst) 没有获取正确的记录。它返回 0 条记录。但是,如果我将 @Query 与本机查询一起使用,它就可以正常工作。

知道为什么派生方法不获取记录吗? 我确保变量名称“batchId”和“institute”在派生方法中拼写正确。 我无法通过在控制台中打开 JPA show sql 来解决任何问题

以下是我的实体详细信息 ....

@Entity
@Table(name = "Batch")
public class BatchEntity implements Serializable{
    
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long batchId;
    
    private String batchName;
    
    @OneToOne
    private ClassEntity clazz;
    
    @ManyToOne(targetEntity = InstituteEntity.class)
    @JoinColumn(name = "instId")
    @JsonIgnoreProperties({"batchList", "classList"})
    private InstituteEntity institute;
    
}

@Entity
@Table(name = "Institute")
public class InstituteEntity implements Serializable{
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long instId;
    
    private String instituteName;
    
    @OneToMany(targetEntity=BatchEntity.class, mappedBy="institute", fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JsonIgnoreProperties("institute")
    private List<BatchEntity> batchList;
    
}


@Repository
public interface BatchRepository extends JpaRepository<BatchEntity, Long>{
    
    
    Optional<BatchEntity> findByBatchIdAndInstitute(Long batchId, InstituteEntity institute);
    
    @Query(value = 
            "SELECT * FROM batch b, institute i WHERE b.batch_id = :batchId AND i.inst_id = :instituteId", 
            nativeQuery = true)
    Optional<BatchEntity> findByBatchIdAndInstituteId(@Param("batchId") Long batchId, @Param("instituteId") Long instituteId);
    
}

JPA sql 日志详细信息 ....

select institutee0_.inst_id as inst_id1_3_0_, institutee0_.institute_name as institut2_3_0_ from institute institutee0_ where institutee0_.inst_id=?
select batchentit0_.batch_id as batch_id1_0_, batchentit0_.batch_name as batch_na2_0_, batchentit0_.clazz_class_id as clazz_cl3_0_, batchentit0_.inst_id as inst_id4_0_ from batch batchentit0_ where batchentit0_.batch_id=? and batchentit0_.inst_id=?

【问题讨论】:

    标签: spring-boot hibernate spring-data-jpa


    【解决方案1】:

    我不认为 Spring Data JPA 派生查询真正使用复杂对象作为方法的参数。 Spring Data JPA 能够处理的是嵌套数据,因此请尝试以下操作:

    Optional<BatchEntity> findByBatchIdAndInstituteInstId(Long batchId, Long instituteId);
    

    【讨论】:

    • 这也为我获取了 0 条记录。我遇到过包含有效参数的派生查询。例如。 public Chapters findByChapterIdAndSubjectAndClassType(Long chapterId, Subject subject, ClassType classType);在这种情况下,关系是 OneToOne。但如果我遇到问题,那就是 OneToMany 和 ManyToOne 双向关系
    • 这确实很奇怪,它应该可以工作。如果您只使用findByBatchId(Long batchId)findByInstituteInstId(Long instituteId),它会获取任何信息吗?
    • findByBatchId(Long batchId) 工作正常。 findByInstituteInstId(Long InstituteId) 不起作用。我不确定这种语法是否正确。 findByInstitute(Institute Institute) 不起作用。这是我真正关心的问题。是否与 Institute 和 Batch 之间的关系有关。由于 findByChapterIdAndSubjectAndClassType(Long chapterId, Subject subject, ClassType classType) 可以很好地处理“复杂”参数 Subject 和 ClassType 我希望 Institute 也能正常工作。
    • 语法没问题。我开始认为问题在于实体如何相互关联,但我在代码中找不到任何似乎错误的内容。
    【解决方案2】:
    @Query(value = 
            "SELECT * FROM batch b, institute i WHERE b.batch_id = :batchId AND i.inst_id = :instituteId", 
            nativeQuery = true)
    

    看看这个本机查询是否正确,然后查询从数据库中获取 SELECT *(all) 记录。 *(all) 表示您应该从此查询中获得 List&lt;BatchEntity&gt;

    声明List&lt;BatchEntity&gt;,我认为它应该可以解决问题

    【讨论】:

    • 如果我没记错*代表所有列。查询结果可能会也可能不会给出一组行。上面的查询对我来说工作正常,输出为 Optional
    猜你喜欢
    • 2018-02-26
    • 2014-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多