-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi there. I have a file (.zul) that sends data from a form with intbox and textbox to a Controller. In this Controller, I need to perform a search for "Ndoc" to check if the client is not in the database. The intbox containing "Ndoc" called "intdoc". If the client is embedded in the database, I want to display a message reporting that. I hope you can help me. I send the code that could perform. This is what I could do:
@Command
@NotifyChange("clienteModel")
public void onClick$btnAddCliente(ForwardEvent fe, @BindingParam("clie") Cliente cli) throws DAOException {
List<Cliente> listaClientes = ClienteDAO.findall();
// List<Cliente> listaClientes = new ArrayList<Cliente>();
// listaClientes.add(ClienteDAO.findall());
boolean encontrado = false;
for (Cliente cliente: listaClientes) {
// if (cliente.getNdoc() == intdoc.getValue()) {
if (cliente.getNdoc() == cli.getNdoc()) {
encontrado = true;
break; // dejo el loop si encuentro el DNI del cliente
}
}
if (!encontrado) {
Cliente cliente = new Cliente();
cliente.setNdoc(intdoc.getValue());
cliente.setApellido(txtapellido.getValue());
cliente.setNombre(txtnombre.getValue());
ClienteService.altaCliente(cliente);
List<Cliente> client = getClienteModel();
setCurrentCliente(client);
clienteModel.add(cliente);
Executions.sendRedirect("/cliente.zul");
} else{
alert("Mensaje.");
}
}
En español: Buenos días. Tengo un archivo (.zul) que envía datos desde un formulario con intbox y textbox a un controller. En esta clase controller necesito realizar una busqueda por "Ndoc", para verificar si el cliente no esta en la base de datos. El intbox que contiene el Ndoc se llama "intdoc". Si es que el cliente está inserto en la base de datos, deseo mostrar un mensaje avisando esto. Espero me puedan ayudar. Les paso parte del código que pude armar.
I'm guessing that cliente.getNdoc()
returns an Integer
. So take a look at your comparison row:
if (cliente.getNdoc() == cli.getNdoc()) {
Basic Java: you are checking if two references points on seam heap area, which is wrong. Change it with
if (cliente.getNdoc().equals(cli.getNdoc())) {
Hi there,
I think you do to much in java in stead let your DB do the work. Update you DAO with this method :
public Client findByNdoc(int Ndoc);
If nothing found => return null.
If something found => return what you have found.
So the code will be then :
if (!ClienteDAO.findByNdoc(cliente.getNDoc()!=null) {
Cliente cliente = new Cliente();
cliente.setNdoc(intdoc.getValue());
cliente.setApellido(txtapellido.getValue());
cliente.setNombre(txtnombre.getValue());
ClienteService.altaCliente(cliente);
List<Cliente> client = getClienteModel();
setCurrentCliente(client);
clienteModel.add(cliente);
Executions.sendRedirect("/cliente.zul");
} else {
alert("Mensaje.");
}
In your approach you iterate over all your clients.
As long you don't have much clients this will go fast.
Take the client list of IKEA world wide, well on a normal server your server will die on permgen.
If he can survive the memory allocation for all those clients => you will still have to iterate over all of them so make your system slow.
Mine approach will return 1 Client or no Client from the DB.
Less code in your viewmodel.
Less memory allocation and no iteration.
If the DB is good indexed the search can be very quick(I have a table with 6M entries, a search for 1 is done in less then a second).
Greetz chill.
It's solved! My mistake was in the way used to compare. Simply replace "==" by method ".equals ()". Thank you very much both!
Asked: 2014-09-02 15:06:57 +0800
Seen: 17 times
Last updated: Sep 04 '14