Last active
September 9, 2020 09:23
-
-
Save SergioDiniz/412ab7cf97e51ccf924dcfeb7c493073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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