Alguém sabe como transformar uma consulta JDBC usando lista para o JPA com hibernate. Fiz alguns estudos na documentação como criteria e o JPQL mas não tive muito sucesso.
//consulta usando JDBC
public List listar_dados(String UF, String tipo, Object periodo) {
conectar();
List lista_dados = new ArrayList();
NumberFormat formatador = new DecimalFormat("#0.0");
NumberFormat formatador2 = new DecimalFormat("#,##0");
try {
statement = conexao
.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultset = statement
.executeQuery("SELECT * FROM presenca where UF ='" + UF
+ "' and periodo = '" + periodo.toString()
+ "' and tipo= '" + tipo + "'");
while (resultset.next()) {
Double assistidos = new Double(resultset
.getString("assistidos"));
Double desassistidos = new Double(resultset
.getString("desassistidos"));
HashMap hashmap_dados = new HashMap();
hashmap_dados.put("instituicao", resultset
.getString("instituicao"));
String assist = formatador2.format(assistidos);
hashmap_dados.put("assistidos", assist);
String desassist = formatador2.format(desassistidos);
hashmap_dados.put("desassistidos", desassist);
double aux_perc = 100 * assistidos
/ (desassistidos + assistidos);
String percentual = formatador.format(aux_perc) + "%";
hashmap_dados.put("percentual", percentual);
lista_dados.add(hashmap_dados);
}
} catch (SQLException Banco) {
JOptionPane.showMessageDialog(null,
"Erro na manipulação de banco: " + Banco);
}
desconectar();
return lista_dados;
}
//consulta usando JPQL que não deu muito certo
@SuppressWarnings("unchecked")
public List<Object[]> BuscarTodos(String UF, String tipo, Object periodo) {
EntityManager em = new JPAUtil().getEntityManager();
Query query = em.createNativeQuery("SELECT * FROM presenca where UF ='" + UF
+ "' and periodo = '" + periodo.toString()
+ "' and tipo= '" + tipo + "'");
List<Object[]> lista = query.getResultList();
System.out.println(lista.get(0)[0]);
em.close();
return lista;
}
DROP TABLE IF EXISTS `presenca`;
CREATE TABLE `presenca` (
`UF` varchar(2) DEFAULT NULL,
`instituicao` varchar(45) DEFAULT NULL,
`assistidos` int(10) unsigned DEFAULT NULL,
`desassistidos` int(10) unsigned DEFAULT NULL,
`periodo` varchar(6) DEFAULT NULL,
`tipo` tinyint(1) DEFAULT NULL,
`id` bigint(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `presenca` VALUES ('AC', 'BANCO DO BRASIL', '17', '5', '201009', '1', '0');
INSERT INTO `presenca` VALUES ('DF', 'BRADESCO', '22', '0', '201009', '1', '0');
INSERT INTO `presenca` VALUES ('AM', 'CEF', '22', '0', '201009', '1', '0');
INSERT INTO `presenca` VALUES ('PI', 'ITAÚ UNIBANCO', '2', '20', '201009', '1', '0');
INSERT INTO `presenca` VALUES ('RS', 'SANTANDER', '3', '19', '201009', '1', '0');
Se alguém poder me ajudar não estou conseguindo utilizar qualquer consulta com critérios usando o JPA.