Hi all:
Dear , I created a crud with hibernate and Viritin 'm using , but I think my project 100 'm only using 25% Viritin , that agraeceria of your observations so that my crud use all the benefits of great Viritin . I leave all my code and screenshot.
HibernateUtil
package com.utileria;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.SessionFactory;
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
Model Usuario:
[code]
package com.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Usuario {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private int id;
private String nombre;
private String apepat;
private String apemat;
private String fono;
private String state;
public Usuario() {
}
public Usuario(int id, String nombre, String apepat, String apemat, String fono, String state) {
this.id = id;
this.nombre = nombre;
this.apepat = apepat;
this.apemat = apemat;
this.fono = fono;
this.state = state;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public String getApepat() {
return apepat;
}
public void setApepat(String apepat) {
this.apepat = apepat;
}
public String getApemat() {
return apemat;
}
public void setApemat(String apemat) {
this.apemat = apemat;
}
public String getFono() {
return fono;
}
public void setFono(String fono) {
this.fono = fono;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
}
[/code]UsuarioDAO
[code]
package com.dao;
import com.model.Usuario;
import com.utileria.HibernateUtil;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class UsuarioDAO {
public void crear(Usuario c){
SessionFactory sf=null;
Session sesion=null;
Transaction tx=null;
try{
sf=HibernateUtil.getSessionFactory();
sesion=sf.openSession();
tx=sesion.beginTransaction();
sesion.save(c);
tx.commit();
sesion.close();
}catch(Exception ex){
}
}
public void editar(Usuario c){
SessionFactory sf=null;
Session sesion=null;
Transaction tx=null;
try{
sf=HibernateUtil.getSessionFactory();
sesion=sf.openSession();
tx=sesion.beginTransaction();
sesion.update(c);
tx.commit();
sesion.close();
}catch(Exception ex){
}
}
public List buscarPorNombre(String nombre){
SessionFactory sf=HibernateUtil.getSessionFactory();
Session sesion=sf.openSession();
Query consultaSQL=sesion.createQuery("from Usuario where nombre='"+nombre+"'");
List<Usuario> lista=consultaSQL.list();
return lista;
}
public List buscarPorId(int id){
SessionFactory sf=HibernateUtil.getSessionFactory();
Session sesion=sf.openSession();
Query consultaSQL=sesion.createQuery("from Usuario where id='"+id+"'");
List<Usuario> lista=consultaSQL.list();
return lista;
}
public void eliminar(Usuario c){
SessionFactory sf=null;
Session sesion=null;
Transaction tx=null;
try{
sf=HibernateUtil.getSessionFactory();
sesion=sf.openSession();
tx=sesion.beginTransaction();
sesion.delete(c);
tx.commit();
sesion.close();
}catch(Exception ex){
}
}
public List mostrarTodo(){
SessionFactory sf=HibernateUtil.getSessionFactory();
Session sesion=sf.openSession();
Query consultaSQL=sesion.createQuery("from Usuario");
List<Usuario> lista=consultaSQL.list();
return lista;
}
}
[/code]Main
package com.vaadin.ricardo.proyecto.hibernatecontainer;
import com.dao.UsuarioDAO;
import com.model.Usuario;
import javax.servlet.annotation.WebServlet;
import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.annotations.Widgetset;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.CssLayout;
import com.vaadin.ui.FormLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
import java.util.List;
import org.vaadin.viritin.fields.MTable;
@Theme("mytheme")
@Widgetset("com.vaadin.ricardo.proyecto.hibernatecontainer.MyAppWidgetset")
public class MyUI extends UI {
Integer id = 0;
@Override
protected void init(VaadinRequest vaadinRequest) {
VerticalLayout vertical = new VerticalLayout();
HorizontalLayout horizontal = new HorizontalLayout();
horizontal.setMargin(true);
horizontal.setSpacing(true);
CssLayout css = new CssLayout();
Button btnNew = new Button("New");
Button btnDel = new Button("Delete");
final Label lblSelected = new Label();
lblSelected.setCaption("Elemento Seleccionado:" + id);
css.addComponent(btnNew);
css.addComponent(btnDel);
css.addComponent(lblSelected);
vertical.addComponent(css);
final UsuarioDAO uDAO = new UsuarioDAO();
final Usuario user = new Usuario();
MTable<Usuario> usuarios = new MTable<>().setBeans(uDAO.mostrarTodo());
Table tabla = new Table("List of User");
tabla.setContainerDataSource(usuarios);
tabla.setSelectable(true);
tabla.setSizeFull();
vertical.addComponent(css);
vertical.addComponent(tabla);
////////////////////////////////////
final FormLayout formulario = new FormLayout();
final TextField txtNombre = new TextField("nombre");
final TextField txtApepat = new TextField("apepat");
final TextField txtApemat = new TextField("apemat");
final TextField txtFono = new TextField("fono");
final TextField txtState = new TextField("state");
Button btnAdd = new Button("Save");
formulario.addComponent(txtNombre);
formulario.addComponent(txtApepat);
formulario.addComponent(txtApemat);
formulario.addComponent(txtFono);
formulario.addComponent(txtState);
formulario.addComponent(btnAdd);
horizontal.addComponent(vertical);
horizontal.addComponent(formulario);
formulario.setVisible(false);
this.setContent(horizontal);
btnNew.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
id = 0;
lblSelected.setCaption("Elemento Seleccionado:" + id);
txtNombre.setValue("");
txtApepat.setValue("");
txtApemat.setValue("");
txtFono.setValue("");
txtState.setValue("");
formulario.setVisible(true);
}
});
btnDel.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
user.setId(id);
uDAO.eliminar(user);
formulario.setVisible(false);
}
});
btnAdd.addClickListener(new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
if (id == 0) {
user.setNombre(txtNombre.getValue());
user.setApepat(txtApepat.getValue());
user.setApemat(txtApemat.getValue());
user.setFono(txtFono.getValue());
user.setState(txtState.getValue());
uDAO.crear(user);
formulario.setVisible(false);
} else {
user.setId(id);
user.setNombre(txtNombre.getValue());
user.setApepat(txtApepat.getValue());
user.setApemat(txtApemat.getValue());
user.setFono(txtFono.getValue());
user.setState(txtState.getValue());
uDAO.editar(user);
formulario.setVisible(false);
}
}
});
tabla.addItemClickListener(new ItemClickEvent.ItemClickListener() {
@Override
public void itemClick(ItemClickEvent itemClickEvent) {
id = (Integer) itemClickEvent.getItem().getItemProperty("id").getValue();
lblSelected.setCaption("Elemento Seleccionado:" + id);
List<Usuario> c = uDAO.buscarPorId(id);
for (int i = 0; i < c.size(); i++) {
txtNombre.setValue(c.get(i).getNombre());
txtApepat.setValue(c.get(i).getApepat());
txtApemat.setValue(c.get(i).getApemat());
txtFono.setValue(c.get(i).getFono());
txtState.setValue(c.get(i).getState());
}
formulario.setVisible(true);
}
});
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}