problems with query in hql

0

I'm doing a registration project. The goal is to register and consult clients and employees, however, it is already possible to save the data, the problem is to consult. for query I made JPanel forms (I'm using netbeans), where to query one must fill in the name and mail field for both forms and then just click the consult button.

The code that I inserted into the browse button is as follows:

ClienteControle cc = new ClienteControle();
        try {
            List<Cliente> ListaDeClientes = cc.buscar(txbNome.getText(),
                    txbEmail.getText());
            DefaultTableModel model =
                    (DefaultTableModel) tbResultados.getModel();
            for (int i = model.getRowCount() - 1; i >= 0; i--) {
                model.removeRow(i);
            }
            if (ListaDeClientes != null) {
                for (int i = 0; i < ListaDeClientes.size(); i++) {
                    Cliente cliente = ListaDeClientes.get(i);
                    String[] c = new String[]{
                        cliente.getNome(),
                        cliente.getEmail()};
                    model.insertRow(i, c);
                }
            }
            tbResultados.setModel(model);
        } catch (Exception e) {
            JOptionPane.showMessageDialog(this,
                    "Não foi possível realizar a consulta de clientes!\n\n"
                    + e.getLocalizedMessage());
        }

In the code above I instantiated the ClientControl class that has the following method:

public List<Cliente> buscar(String nome, String email)
            throws Exception {
        return null;
    }

Then I created the following method within the ClientData class so that I could query the connection to the database:

public List<Cliente> getListaDeClientes(String nome, String email)
            throws Exception {
       EntityManager em = getEntityManager();
       try {
           String hql ="";
           if(nome != null && !nome.equals("")) {
               hql += " WHERE nome like upper (:nome)";
           }
           if (email != null && !email.equals("")) {
               if (!hql.equals("")) {
                   hql += " AND email like lower(:email)";
               } else {
                   hql += " WHERE email like lower(:email)";
               }
           }
           hql = "FROM Cliente" + hql;
           Query q = em.createQuery(hql);
           if(nome != null && !nome.equals("")) {
               q.setParameter("nome", "%" +
                       nome.toLowerCase() + "&");
           }
           if (email != null & !email.equals("")) {
               q.setParameter("email", "%" +
                       email.toLowerCase() + "%");
           }
           return q.getResultList();
       } finally {
           em.close();
       }
    }

And then I changed the Search method of ClientControl to:

public List<Cliente> buscar(String nome, String email)
            throws Exception {
        return new ClienteDAO().getListaDeClientes(nome, email);
    }

The button should run normally but these error messages appear:

  

Exception Description: An exception was thrown while searching for   pesistence archives with ClassLoader:   sun.misc.Launcher$AppClassLoader@5c647e05

     

Internal Exception: javax.persistence.PersistenceException: Exception   [EclipseLink-28018] (Eclipse Persistense Services -   2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.EntityManagerSetupException

     

Exception Exception Description: Predeployment of   PersistenceUnit [CadastroPU]   failed.org.eclipse.persistence.exceptions.ValidationException

     Internal Exception: Exception [EclipseLink-7299] (Eclipse Persistence   Services - 2.5.2.v20140319-9ad6abd)

     

Exception Description: Conflicting annotations with the same name   [SEQ_STORE] were found. the first one   [@javax.persistence.SequenceGenerator ({allocationSize = 20,   name = SEQ_STORE, sequenceName = semi-official})] was found within   [class cadastro.Pessoa.Funcionario] and the second   [@javax.persistence.SequenceGenerator ({allocationSize = 20,   name = SEQ_STORE, sequenceName = client_seq})] was found within [class   registration.Person.Customer]. Named annotations must be unique across the   persistence unit.

Why does this appear? can you solve?

    
asked by anonymous 13.03.2018 / 20:20

2 answers

2

Your error is clear on this line:

  

Exception Description: Conflicting annotations with the same name [SEQ_STORE] were found . the first one [@ javax.persistence.SequenceGenerator ({allocationSize = 20, name = SEQ_STORE, sequenceName = servername]) was found within [class cadastro.Pessoa.Funcionario] and the second [@ javax.persistence.SequenceGenerator ({allocationSize = 20, name = SEQ_STORE, sequenceName = client_seq})] was found within [class register.Person.Customer]. Named annotations must be unique across the persistence unit.

See what you called SEQ_STORE for two different sequences: se_seq and seq_seq. To adjust it is necessary only to rename one of them, for example:

... ({allocationSize=20, name=SEQ_STORE_FUNCIONARIO, sequenceName= funcionario_seq})
    
13.03.2018 / 20:27
0

Good morning Luan, as you changed the name of the sequence and presented another exception you need to declare an identifier for your entity in the query, try this:

hql += " WHERE c.nome like upper (:nome)";


if (!hql.equals("")) {
                   hql += " AND c.email like lower(:email)";
               } else {
                   hql += " WHERE c.email like lower(:email)";
               }

hql = "FROM Cliente c" + hql;
    
14.03.2018 / 12:37