(Incomparable Types: String and Order) Compare an entry in JTextField with the database in JPA

0

I try to make a way that the value entered by the user can be compared with the data of a database column or a JTable (not what is better for that purpose), for example: when the input of the control number to be: 123456 and in the coluna numControle of the base loja have some data with that value the user does not let insert. Here you follow my solution:

public class MySQLPedidoDAO implements PedidoDAO {

    //null pointer exception pois esse campo estava inicializado como null
    public ConnectionFactory cf;
    
    public MySQLPedidoDAO(ConnectionFactory cf) {
        this.cf = cf;
    }
    
    /*... algunos metodos del crud JPA...*/

/*
@Override
    public List<Pedido> getNumControle(){
       cf = new ConnectionFactory();
       cf.createEm().getTransaction().begin();
       Query consulta = cf.createEm().createQuery("select numControle from Pedido p");
       List <Pedido> pedidos = consulta.getResultList();
       cf.createEm().getTransaction().commit();
       cf.close();
       return pedidos;
    }*/

@Override
    public Integer<Pedido> getNumControle(){
       cf = new ConnectionFactory();
       cf.createEm().getTransaction().begin();
       Query consulta = cf.createEm().createQuery("select numControle from Pedido p");
       List <Integer> pedidos = consulta.getResultList();
       cf.createEm().getTransaction().commit();
       cf.close();
       return pedidos;
    }
    
}

ViewPedidos:

public class ViewPedidos extends javax.swing.JInternalFrame {

    private DAOManager manager;
    private PedidoModel model;
    private MySQLPedidoDAO mspdao;
    private MySQLClienteDAO mscdao;
    private MySQLProdutoDAO msprdao;
    private List<Produto> produtos;
    private List<Pedido> pedidos;
    private Pedido ped;
    private Produto pro;
    private boolean editavel;

    /**
     * @return the ped
     */
    public Pedido getPed() {
        return ped;
    }

    /**
     * @param ped the ped to set
     */
    public void setPed(Pedido ped) {
        this.ped = ped;
    }

    /**
     * @return the editavel
     */
    public boolean isEditavel() {
        return editavel;
    }



public void inserirDados(Pedido ped) {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
        Date date = new Date();
        ConnectionFactory cf = new ConnectionFactory();
        mspdao = new MySQLPedidoDAO(cf);
        System.out.println(dateFormat.format(date));
        //Acá recupero los valores de la función en una lista
        List<Integer> pedido = mspdao.getNumControle();


    /* ... inserciones en JTextField arriba ... */
        if(tfDataCad.getText().length() == 0){
        ped.setDataCadastro(date);
        } else {
        ped.setDataCadastro(new java.sql.Date(((java.util.Date) tfDataCad.getValue()).getTime()));
        } 
        ped.setNomeProduto(cbNomeProduto.getSelectedItem().toString());
        ped.setNumControle(Integer.parseInt(tfNumControle.getText()));
 for (int i = 0; i < pedido.size(); i++) {
       // Pedido get = pedido.get(i);
Integer get = pedido.get(i);
/* Error abajo, Incomparable types: String and Integer */
        if(tfNumControle.getText() == get){
        JOptionPane.showMessageDialog(rootPane, "Inserção não realizada, Numero de controle já cadastrado", "Cadastro de Pedidos", JOptionPane.INFORMATION_MESSAGE);
        } else {
          //insertar
        }
    }
}

And the stacktrace:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.Integer cannot be cast to com.exemplo.model.Pedido
	at com.exemplo.view.ViewPedidos.inserirDados(ViewPedidos.java:549)
	at com.exemplo.view.ViewPedidos.btInserirActionPerformed(ViewPedidos.java:409)
	at com.exemplo.view.ViewPedidos.access$300(ViewPedidos.java:51)
	at com.exemplo.view.ViewPedidos$4.actionPerformed(ViewPedidos.java:283)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
	at java.awt.Component.processMouseEvent(Component.java:6533)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
	at java.awt.Component.processEvent(Component.java:6298)
	at java.awt.Container.processEvent(Container.java:2236)
	at java.awt.Component.dispatchEventImpl(Component.java:4889)
	at java.awt.Container.dispatchEventImpl(Container.java:2294)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
	at java.awt.Container.dispatchEventImpl(Container.java:2280)
	at java.awt.Window.dispatchEventImpl(Window.java:2746)
	at java.awt.Component.dispatchEvent(Component.java:4711)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
	at java.awt.EventQueue.access$500(EventQueue.java:97)
	at java.awt.EventQueue$3.run(EventQueue.java:709)
	at java.awt.EventQueue$3.run(EventQueue.java:703)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
	at java.awt.EventQueue$4.run(EventQueue.java:731)
	at java.awt.EventQueue$4.run(EventQueue.java:729)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

I am still an initiate in JPA, with resultSet () I remember that it is different just by putting the rs.next () method, there I made a for to percider the list but you are not comparing and return that incompatibility error.

    
asked by Victor G. 10.04.2017 в 18:18
source

2 answers

0

You are wrong in your query select numControle from Order p this returns you data of type integer not of your type object Order , you have two options to change your query to if :

select p from Pedido p

This will return the query to your object type request .

or

Receive that list of integers as follows:

List <Integer> pedidos = consulta.getResultList();

but this implies that your method returns data of integer type.

@Override
    public List<Integer> getNumControle(){
       cf = new ConnectionFactory();
       cf.createEm().getTransaction().begin();
       Query consulta = cf.createEm().createQuery("select numControle from Pedido p");
       List <Integer> pedidos = consulta.getResultList();
       cf.createEm().getTransaction().commit();
       cf.close();
       return pedidos;
    }
    
answered by 10.04.2017 в 19:56
0

You can try to go through it in the following way:

for(Pedido pedGet: pedido){
//Aquí obtienes tu valores
int numeroN = pedGet.getValor();
//Y lo comparas para que no lo deje insertar
if(numControle == numeroN){
//No inserto
}else{
//Inserto
}
}

I edit my answer:

You can not compare an Integer with a String

if(tfNumControle.getText() == get){

You must change your get to a String and compare them with the equals, in the following way:

 if(tfNumControle.getText().equals(String.valueOf(get))){

Good luck!

    
answered by 10.04.2017 в 19:42