Good morning I'm trying to make a loan system in which by typing an ID number inside a p: InputText through ajax it performs a search in the database and it gives me a Client idea is that when I look for this client I return a list of loans that have been made and show them in a p: dataTable , what I have done and I do not know why it does not work is the following :
<h:form id="formPrincipal">
<p:growl life="5000" showDetail="true" showSummary="true" autoUpdate="true"/>
<p:panel header="PRESTAMOS" style="text-align: center; font-weight: bold; width: 1000px;
margin: 0 auto;">
<p:layout id="idDatosCliente" style="width: 100%; height: 185px">
<p:layoutUnit position="center" style="padding: 5px; text-align: center;
font-size: 14px;" size="300">
<p:inputText placeholder="Buscar clientes - ingresa aquí el número de cedula"
size="100" value="#{prestamoBean.cedula}">
<p:ajax listener="#{prestamoBean.buscarCliente()}" event="keyup"
update=":formPrincipal:gridCliente :formPrincipal:tblPrestamo"/>
</p:inputText>
<p:commandButton icon="ui-icon-search" oncomplete="PF('dialogCrear').show();"
update=":formCrear:dlgCrear"/>
<p:separator/>
<p:panelGrid columns="4" id="gridCliente"
style="width: 100%; text-align: justify;">
<h:outputText value="NOMBRE: "/>
<h:outputText value="#{prestamoBean.cliente.nombreCliente}"/>
<h:outputText value="EMPRESA: "/>
<h:outputText value="#{prestamoBean.cliente.empresa.codigoEmpresa}"/>
<h:outputText value="TASA DE INTERES: "/>
<h:outputText value="#{prestamoBean.cliente.tasaInteres} %"/>
<h:outputText value="No. CUENTA: "/>
<h:outputText value="#{prestamoBean.cliente.numeroCuenta}"/>
</p:panelGrid>
<p:separator/>
<div id="cabeceraDiv">
ACCIONES
</div>
<h:panelGrid columns="3" style="width: 100%; text-align: center">
<p:commandButton value="GENERAR PRESTAMO" onclick="PF('dialogNuevoPrestamo').show();"/>
<p:commandButton value="PAGO DE INTERESES"/>
<p:commandButton value="PAGO CAPITAL"/>
</h:panelGrid>
</p:layoutUnit>
</p:layout>
<P></P>
<p:dataTable id="tblPrestamo" emptyMessage="NO HAY PRESTAMOS PENDIENTES"
value="#{prestamoBean.listaPrestamo}"
var="r">
<p:column headerText="FECHA">
<h:outputText value="${r.fecha}"/>
</p:column>
<p:column headerText="DESCRIPCION">
<h:outputText value="${r.concepto}"/>
</p:column>
<p:column headerText="DEBE">
<h:outputText value="${r.debe}"/>
</p:column>
<p:column headerText="HABER">
<h:outputText value="${r.haber}"/>
</p:column>
<p:column headerText="SALDO">
<h:outputText value="${r.saldo}"/>
</p:column>
</p:dataTable>
</p:panel>
</h:form>
and this is the Bean I'm doing, which is missing a lot but I'm going step by step and this is very important for me:
public List<Prestamo> getListaPrestamo() {
return listaPrestamo;
}
public void setListaPrestamo(List<Prestamo> listaPrestamo) {
this.listaPrestamo = listaPrestamo;
}
//Metodo que devuelve los prestamos
public void ObtenerPrestamos() {
this.session = null;
Integer codigoCliente = this.cliente.getCodigoCliente();
prestamoDao pDao = new PrestamoDaoImp();
this.listaPrestamo = pDao.BuscarIDCliente(this.session, codigoCliente);
}
//Busqueda Instantanea de clientes
public void buscarCliente() {
this.session = null;
this.transaction = null;
try {
if (this.cedula == null) {
return;
}
clienteDao cDao = new clienteDaoImp();
this.session = HibernateUtil.getSessionFactory().openSession();
this.transaction = session.beginTransaction();
this.cliente = cDao.BuscarCedula(this.session, cedula);
if (this.cliente != null) {
this.cedula = null;
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Correcto", "Cliente encontrado"));
ObtenerPrestamos();
} else {
this.cedula = null;
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Cliente no encontrado"));
}
this.transaction.commit();
} catch (Exception e) {
if (this.transaction != null) {
this.transaction.rollback();
}
} finally {
if (this.session != null) {
this.session.close();
}
}
}
//BUsqueda normal de clientes por medio de botón recibiendo parametro cedula
public void buscarCliente2(String cedula) {
this.session = null;
this.transaction = null;
try {
if (this.cedula == null) {
return;
}
clienteDao cDao = new clienteDaoImp();
this.session = HibernateUtil.getSessionFactory().openSession();
this.transaction = session.beginTransaction();
this.cliente = cDao.BuscarCedula(this.session, cedula);
if (this.cliente != null) {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Correcto", "Cliente encontrado"));
ObtenerPrestamos();
} else {
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error", "Cliente no encontrado"));
}
this.transaction.commit();
} catch (Exception e) {
if (this.transaction != null) {
this.transaction.rollback();
}
} finally {
if (this.session != null) {
this.session.close();
}
}
}
The method
GetPreset ()
is the one that searches through the customer's code for the list of loans to fill the variable
listaPrestamo
Besides this I also publish the method that I use to look up the client code in the database and return a list, if there is where I have the problem and I have not noticed.
public List<Prestamo> BuscarIDCliente(Session session,Integer codigoCliente) {
List<Prestamo> lista = null;
session = HibernateUtil.getSessionFactory().openSession();
Transaction t = session.beginTransaction();
String hql = "FROM Prestamo WHERE codigoCliente = :codigoCliente";
try {
lista = session.createQuery(hql).list();
t.commit();
session.close();
} catch (Exception e) {
t.rollback();
}
return lista;
}
I have updated the question adding this time the way I do to fill the list, I'm probably doing something wrong. Thanks