Skip to content

Instantly share code, notes, and snippets.

@ajfmo
Created December 31, 2017 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ajfmo/d166d16aae2061e3edad71185b0a61e8 to your computer and use it in GitHub Desktop.
Save ajfmo/d166d16aae2061e3edad71185b0a61e8 to your computer and use it in GitHub Desktop.
DAO for Student
package ajfmo.studycontrol.DAO;
import java.util.List;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import ajfmo.studycontrol.entities.Career;
import ajfmo.studycontrol.entities.Section;
import ajfmo.studycontrol.entities.Student;
import ajfmo.studycontrol.utils.HibernateUtil;
public class StudentDAO {
private final Session session = HibernateUtil.getSessionFactory();
private Transaction transaction = null;
private CriteriaBuilder builder = session.getCriteriaBuilder();
private Student student;
public void createStudent(String studentId, String studentName, Career career, Section section) {
student = new Student(studentId, studentName, career, section);
try {
transaction = session.beginTransaction();
System.out.println(studentId);
System.out.println(studentName);
System.out.println(career);
System.out.println(section);
session.save(student);
transaction.commit();
} catch (HibernateException e) {
e.printStackTrace();
}
session.close();
}
public List<Student> studentCriteria() {
CriteriaQuery<Student> criteria = builder.createQuery(Student.class);
Root<Student> root = criteria.from(Student.class);
criteria.select(root);
List<Student> resultset = session.createQuery(criteria).getResultList();
return resultset;
}
public List<Career> careerCriteria() {
CriteriaQuery<Career> criteria = builder.createQuery(Career.class);
Root<Career> root = criteria.from(Career.class);
criteria.select(root);
List<Career> resultset = session.createQuery(criteria).getResultList();
return resultset;
}
public List<Section> sectionCriteria() {
CriteriaQuery<Section> criteria = builder.createQuery(Section.class);
Root<Section> root = criteria.from(Section.class);
criteria.select(root);
List<Section> resultset = session.createQuery(criteria).getResultList();
return resultset;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment