我正在使用jpa更新java代码中的数据库表,如下所示: 我尝试过使用事务(传播和新事务),但它不起作用。 我使用postgres sql作为底层数据库。
while(getCount(Integer.parseInt(year) )>0) {
List<Tls201Appln> eList = (List<Tls201Appln>) em.createNativeQuery(
"select * from Tls201_Appln tls where tls.docdb_family_id in (select b.docdb_family_id from work_table_"+year+"_family_ids b where b.is_processed = 'F' limit 100 )",
Tls201Appln.class).getResultList();
eList.sort((x1, x2)->x1.getEarliestFilingDate().before(x2.getEarliestFilingDate())?1:x1.getEarliestFilingDate().after(x2.getEarliestFilingDate())?-1:0);
Set<Integer> uniqueDocDBIds = eList.stream().map(x ->
x.getDocdbFamilyId()
).collect(Collectors.toSet());
updateTable(uniqueDocDBIds);
String rssFile = "";
try {
rssFile = rssGenerator.generatRSSFromData(eList, year);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
exchange.getIn().setBody(rssFile);
zipper.writeTozip(exchange);
}
// return rssFile;
}
@Transactional(propagation=Propagation.REQUIRED)
private void updateTable(Set<Integer> uniqueDocDBIds) {
String ids = "";
for(Integer id : uniqueDocDBIds) {
ids += ","+id;
}
if(ids.length() >=2) {
ids = ids.substring(1);
String query = "update work_table_"+year+"_family_ids set is_processed='T' where docdb_family_id in (" +ids+")";
int result = em.createNativeQuery(query).executeUpdate();
em.flush();
Logger.getLogger(PatstatService.class).debug("records updated: "+result);
}
}
public long getCount(int year) {
return ((Number)em.createNativeQuery(
"select count(*) from work_table_"+year+"_family_ids where is_processed='F'").getSingleResult()).longValue();
}
代码运行正常且没有错误,但是DB表没有更新:
select count(*) from work_table_1996_family_ids where is_processed='T'
始终返回0.
我做错了什么?