Skip to content

Instantly share code, notes, and snippets.

@SergioDiniz
Last active September 9, 2020 09:23
Show Gist options
  • Save SergioDiniz/412ab7cf97e51ccf924dcfeb7c493073 to your computer and use it in GitHub Desktop.
Save SergioDiniz/412ab7cf97e51ccf924dcfeb7c493073 to your computer and use it in GitHub Desktop.
package dao;
import java.util.List;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import beans.Task;
public class TaskDao {
private Session session;
private SessionFactory factory;
public TaskDao(){
factory = new Configuration().configure().buildSessionFactory();
session = factory.openSession();
}
public void addTask(Task task){
try {
session.beginTransaction();
session.save(task);
session.getTransaction().commit();
} finally {
factory.close();
session.close();
}
}
public List<Task> listarTask(){
List<Task> tasks;
try {
Query query = session.createQuery("from Task");
tasks = query.getResultList();
} finally {
factory.close();
session.close();
}
return tasks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment