gpt4 book ai didi

java - 使用 JPA/Hibernate 过滤 DataSet 中的行

转载 作者:行者123 更新时间:2023-11-30 05:31:53 32 4
gpt4 key购买 nike

我有以下代码,用于从多个表中检索数据(使用联接),然后将每一行映射到 DTOList 中,但我还需要根据用户首选项应用过滤器:每个 table1.name 或 table2.name,表3等

所以我只想知道就性能和最佳实践而言最好的方法是什么;

  1. 检索所有行,然后使用 lambda 应用过滤器(更简单)
  2. 使用 Criteria 或其他方式将查询更改为动态查询?
  3. 还有其他解决方案吗=?
@Repository
public class ArchiveRepository {

@Autowired
EntityManager em;

String queryStr = "select wsr.id as sampleid, s.id as slideid, tb.name as batchname, k.lot_number as kitlot, " +
" 'STRING' as slidetype, tb.worklist_name as worklist, wsr.final_call as results, " +
" wa.final_pattern_ids as patterns, 'edited/yesno' as edited, wsr.last_modified_by as user, wsr.last_modified_date as time " +
" from slide s " +
" left join table2 tb on s.test_batch_id = tb.id " +
" left join table3 k on tb.kit_lot_id = k.id " +
" left join table4 w on s.id = w.slide_id " +
" left join tabl5 pw on pw.well_id = w.id " +
" left join tabl6 cw on cw.well_id = w.id " +
" left join tabl7 wsr on wsr.patient_well_sample_id = pw.id or wsr.control_sample_id = cw.id " +
" left join (select * from *** (other subselect)) wa on wa.well_sample_id = wsr.**id or wa.well_sample_id = wsr.**id " +
"where tb.state = 'STATENEEDED'";

public ArchiveDataListDTO getArchiveData(){
Query query = em.createNativeQuery(queryStr);
ArchiveDataListDTO archiveDataListDTO = new ArchiveDataListDTO();
List<Object[]> resultL = (List<Object[]>)query.getResultList();
for( Object[] o : resultL){
archiveDataListDTO.addArchiveDataRow(
new ArchiveDataDTO((String)o[0], String.valueOf(o[1]), (String) o[2], (String) o[3], (String) o[4], (String) o[5],
(String) o[6], (String) o[7], (String) o[8], (String) o[9], (String) o[10]));
}
return archiveDataListDTO;
}

}

**
note I struggled some with the code cause I wanted to apply @sqlresultsetmapping to avoid manual results mapping but it just didn´t work, most of the examples out there are when you have an entity in the DB but in this case I retrieve from many tables.**

Thanks so much

最佳答案

2 .- 使用 Criteria 或其他方式将查询更改为动态查询?

关于java - 使用 JPA/Hibernate 过滤 DataSet 中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57363322/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com