problems with consultation in hql

Asked

Viewed 73 times

0

I’m doing a criminal record project. The goal is to register and consult customers and employees, however, it is already possible to save the data, the problem is to consult. I made Jpanel forms (I am using netbeans), where to query you must fill in the Name and Email field for both forms and then just click the Refer button.

The code I entered in the query 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 above code I entered the Clientecontrol class which has the following method:

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

Then I created the following method within the Customer class so that I could consult before 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 from Clientecontrol to:

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

The button should run normally but appears these error messages:

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= funcio_seq})] was found Within [class cadastro.Pessoa.Funcionario] and the Second [@javax.persistence.Sequencegenerator({allocationSize=20, name=SEQ_STORE, sequenceName=cliente_seq})] was found Within [class Customer]. Named Annotations must be Unique Across the persistence Unit.

Why does this come up? There’s a way to fix it?

  • According to that mistake: Exception Description: Conflicting annotations with the same name [SEQ_STORE] were found. the first one [@javax.persistence.SequenceGenerator({allocationSize=20, name=SEQ_STORE, sequenceName= funcionario_seq})] was found within [class cadastro.Pessoa.Funcionario] and the second [@javax.persistence.SequenceGenerator({allocationSize=20, name=SEQ_STORE, sequenceName=cliente_seq})] was found within [class cadastro.Pessoa.Cliente]. Named annotations.. Annotations conflicts. put your Client and Employee class or try to change the attribute name of @SequenceGenerator.

2 answers

2

Your mistake is very 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= funcio_seq})] was found Within [class cadastro.Pessoa.Funcionario] and the Second [@javax.persistence.Sequencegenerator({allocationSize=20, name=SEQ_STORE, sequenceName=cliente_seq})] was found Within [class cadastro.Pessoa.Cliente]. Named Annotations must be Unique Across the persistence Unit.

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

... ({allocationSize=20, name=SEQ_STORE_FUNCIONARIO, sequenceName= funcionario_seq})
  • i had already done the procedure, but another exception appeared when creating a query in entityManager, the description is as follows: An Exception occurred while Creating a query in entityManager: Exception Description: Syntax error Parsing [FROM Client WHERE name like upper (:name) AND email like Lower(:email)]. [13,13] An Identification variable must be provided for a range variable declaration. If you can help, I’m grateful

  • for a better understanding, I took this print. https://drive.google.com/file/d/1FrDqMr_z2ZO51zN0tNt3ZkU1LPIt7XJh/view

  • @Luan, in this case is another mistake, so you need to create another question, okay? When create me let me know that I look there to help you.

0

Good morning Luan, as you changed the name of Luan 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;
  • This does not provide an answer to the question. To criticize or request clarification from an author, leave a comment below its publication. - Of Revision

  • I expressed myself badly, in which case the answer was to the commenting that he did reporting another exception.

Browser other questions tagged

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