How to query involving more than one table (JPA) using JPQL

Asked

Viewed 558 times

1

Hello, I have a query using SQL, now I am changing the project using JPA and I would like to change the queries in a coherent way with JPA.

    public void gerarConsulta() {

    jTResultado.getColumnModel().getColumn(0).setMaxWidth(50);
    jTResultado.getColumnModel().getColumn(1).setPreferredWidth(100);
    jTResultado.getColumnModel().getColumn(2).setPreferredWidth(100);
    jTResultado.getColumnModel().getColumn(3).setPreferredWidth(100);
    jTResultado.getColumnModel().getColumn(4).setPreferredWidth(100);
    jTResultado.getColumnModel().getColumn(5).setPreferredWidth(100);
    jTResultado.getColumnModel().getColumn(6).setPreferredWidth(100);
    DefaultTableModel modelo = (DefaultTableModel) jTResultado.getModel();
    modelo.setNumRows(0);

    Statement st;

    try {
        st = con.createStatement();
        PreparedStatement ps = con.prepareStatement("select * from funcionario_ocorrencia " + ""
                + "inner join funcionario on funcionario.funcionario_id = funcionario_ocorrencia.funcionario_id "
                + "inner join setor    on setor.setor_id      = funcionario.setor_id "
                + "inner join ocorrencia    on ocorrencia.ocorrencia_id       = funcionario_ocorrencia.ocorrencia_id   "
                + "inner join empresa    on empresa.empresa_id      = funcionario.empresa_id "
                + "where funcionario_ocorrencia.ocorrencia_data_pg between '" + jFormatDataInicial.getText() + "'" + "and '" + jFormatDataFinal.getText() + "'"
                + " and funcionario.funcionario_id = '" + jTMatricula.getText() + "'");

        ResultSet rs = ps.executeQuery();

        while (rs.next()) {
            int id = rs.getInt("funcionario_id");
            String nome = rs.getString("funcionario_descr");
            String empresa = rs.getString("empresa_descr");
            String setor = rs.getString("setor_descr");
            String ocorrenciaTipo = rs.getString("ocorrencia_descr");
            double ocorrencia_valor = rs.getDouble("ocorrencia_valor");
            String data = rs.getString("ocorrencia_data_pg");
            modelo.addRow(new Object[]{id, nome, empresa, setor, ocorrenciaTipo, ocorrencia_valor, data});
        }


    } catch (Exception e) {
        System.out.println("Problemas ao tentar conectar com o banco de dados");
    }

}

I read some posts, some examples, but is confused, because I had only done the way it is in this code, I think it will be awkward, since I am changing this small project to the JPA specification.If anyone can help, I am grateful.

  • Doubts: does your code give any error or unexpected result? What do you find "inelegant"? Please click on [Edit] and clarify directly on the question.

  • No error, this way works correctly, using SQL . I would like to use something like JPQL, for example, that’s what I meant by "consistent with JPA and that I believe JDBC with JPA would be awkward", I don’t know if it was clearer what I meant.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.