Boa noite pessoal, tudo bem?
Estou precisando persistir diversos registros e queria saber a melhor forma de se fazer isso, hoje estou fazendo da forma abaixo e gostaria de saber se tem alguma maneira melhor pois vai ser processado diversos registros (1000).
Realmente queria a opinião de vocês de como melhorar esse código lembrando que está apenas com alguns campos pois é bem maior que isso a classe.
public Boolean processar(JSONObject json) throws Exception {
this.em = EntityManagerUtil.build().createEntityManager();
EntityTransaction tx = em.getTransaction();
ContasReceberEntity entity = new ContasReceberEntity();
try {
for (int i = 0; i < json.getJSONArray("dtParcela").length(); i++) {
try {
em.clear();
tx.begin();
entity.setIdExcecao(0); //Eu precisei zerar o campo pois como é auto-incremento a entidade sempre mantinha o ultimo gerado.
entity.setIdEmpresa(json.isNull("idEmpresa") ? null : json.getInt("idEmpresa"));
entity.setTpExcecao(json.isNull("tpConta") ? null : json.getString("tpConta"));
entity.setDtExcecao(DateUtil.parse(json.getJSONArray("dtParcela").getString(i), "yyyy-MM-dd'T'HH:mm:ss"));
entity.setDtUltalt(new Date());
em.persist(entity);
tx.commit();
} catch (Exception ex) {
tx.rollback();
return false;
}
}
return true;
} catch (Exception ex) {
return false;
} finally {
if (em != null && em.isOpen()) {
em.close();
}
}
}
Abraço