【发布时间】: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