1. Crear una Entidad llamado UbigeoEntity con los siguientes atributos
private Integer id;
private String cod_dpto;
private String cod_prov;
private String cod_dist;
private String nombre;
y crear los métodos de accedo get y set
2. Crear un interfaz llamada UbigeoDao con los siguientes metodos
public List<UbigeoEntity> findDepartamento();
public List<UbigeoEntity> findProvincia(UbigeoEntity v);
public List<UbigeoEntity> findDistrito(UbigeoEntity v);
3. Crear una clase UbigeoDaoImpl e implementar todos los metos de UbigeoDao el siguiente código
public class UbigeoDaoImpl implements UbigeoDao{
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List<UbigeoEntity> findDepartamento() {
List<UbigeoEntity> listado = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
session.beginTransaction();
Criteria crit = session.createCriteria(UbigeoEntity.class);
crit.add(Restrictions.eq("cod_prov", "00"));
listado = crit.list();
session.beginTransaction().commit();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
session.beginTransaction().rollback();
}
return listado;
}
@Override
public List<UbigeoEntity> findProvincia(UbigeoEntity v) {
List<UbigeoEntity> listado = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
session.beginTransaction();
Criteria crit = session.createCriteria(UbigeoEntity.class);
crit.add(Restrictions.eq("cod_dpto", v.getCod_dpto()));
crit.add(Restrictions.eq("cod_dist", "00"));
crit.add(Restrictions.not(Restrictions.in("cod_prov",new String[] {"00"})));
listado = crit.list();
session.beginTransaction().commit();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
session.beginTransaction().rollback();
}
return listado;
}
@Override
public List<UbigeoEntity> findDistrito(UbigeoEntity v) {
List<UbigeoEntity> listado = null;
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try {
session.beginTransaction();
Criteria crit = session.createCriteria(UbigeoEntity.class);
crit.add(Restrictions.eq("cod_dpto", v.getCod_dpto()));
crit.add(Restrictions.eq("cod_prov", v.getCod_prov()));
crit.add(Restrictions.not(Restrictions.in("cod_dist",new String[] {"00"})));
listado = crit.list();
session.beginTransaction().commit();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
session.beginTransaction().rollback();
}
return listado;
}
@ManagedBean
@SessionScoped
public class RegistroBean {
private List<SelectItem> selectItemsOneDepartamento;
private List<SelectItem> selectItemsOneProvincia;
private List<SelectItem> selectItemsOneDistrito;
public List<SelectItem> getSelectItemsOneDepartamento() {
this.selectItemsOneDepartamento = new ArrayList<SelectItem>();
VideoDao dao = new VideoDaoImpl();
List<UbigeoEntity> p = dao.findDpto();
selectItemsOneDepartamento.clear();
for (UbigeoEntity dpt : p) {
SelectItem dptItem = new SelectItem(dpt.getCod_dpto(), dpt.getNombre());
this.selectItemsOneDepartamento.add(dptItem);
}
return selectItemsOneDepartamento;
}
public void setSelectItemsOneDepartamento(List<SelectItem> selectItemsOneDepartamento) {
this.selectItemsOneDepartamento = selectItemsOneDepartamento;
}
public List<SelectItem> getSelectItemsOneProvincia() {
this.selectItemsOneProvincia = new ArrayList<SelectItem>();
VideoDao dao = new VideoDaoImpl();
List<UbigeoEntity> p = dao.findProvincia(this.selectedEntity);
selectItemsOneProvincia.clear();
for (UbigeoEntity t : p) {
SelectItem dptItem = new SelectItem(t.getCod_prov(), t.getNombre());
this.selectItemsOneProvincia.add(dptItem);
}
return selectItemsOneProvincia;
}
public void setSelectItemsOneProvincia(List<SelectItem> selectItemsOneProvincia) {
this.selectItemsOneProvincia = selectItemsOneProvincia;
}
public List<SelectItem> getSelectItemsOneDistrito() {
this.selectItemsOneDistrito = new ArrayList<SelectItem>();
VideoDao dao = new VideoDaoImpl();
List<UbigeoEntity> p = dao.findDistrito(this.selectedEntity);
selectItemsOneDistrito.clear();
for (UbigeoEntity t : p) {
SelectItem dptItem = new SelectItem(t.getId(), t.getNombre());
this.selectItemsOneDistrito.add(dptItem);
}
return selectItemsOneDistrito;
}
public void setSelectItemsOneDistrito(List<SelectItem> selectItemsOneDistrito) {
this.selectItemsOneDistrito = selectItemsOneDistrito;
}
5 En xtml crear el siguiente código
No hay comentarios:
Publicar un comentario