Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package action; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import hibernate.HibernateUtil; import java.util.ArrayList; import java.util.Date; import java.util.List; import model.Document; import org.hibernate.Session; /** * * @author nerea */ public class DocumentAction extends ActionSupport implements ModelDriven { private Document document = new Document(); private List<Document> documentList; @Override public Object getModel() { return document; } @Override public String execute() throws Exception { return SUCCESS; } /** * @return the documentList */ public List<Document> getDocumentList() { return documentList; } /** * @param documentList the documentList to set */ public void setDocumentList(List<Document> documentList) { this.documentList = documentList; } public String addDocument() throws Exception { Session session = HibernateUtil.getSessionFactory().openSession(); try { document.setCreated_at(new Date()); document.setUpdated_at(new Date()); session.beginTransaction(); session.save(document); session.getTransaction().commit(); this.setDocumentList((List<Document>) session.createCriteria(Document.class).list()); } catch (Exception e) { System.err.println("Error creating User :" + e.getMessage()); session.getTransaction().rollback(); return ERROR; } finally { if (session.isOpen()) { session.close(); } } return SUCCESS; } public String listDocuments() { Session session = HibernateUtil.getSessionFactory().openSession(); this.setDocumentList((List<Document>) session.createCriteria(Document.class).list()); if (session.isOpen()) { session.close(); } return SUCCESS; } }