1 |
package dk.thoerup.hibertest1;
|
2 |
|
3 |
import java.util.List;
|
4 |
|
5 |
import org.hibernate.Transaction;
|
6 |
|
7 |
import org.hibernate.Criteria;
|
8 |
import org.hibernate.classic.Session;
|
9 |
import org.hibernate.criterion.Restrictions;
|
10 |
|
11 |
public class DepartmentDAO {
|
12 |
|
13 |
@SuppressWarnings("unchecked")
|
14 |
public static List<Department> getDepartments() {
|
15 |
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
|
16 |
Transaction tx = session.beginTransaction();
|
17 |
Criteria c = session.createCriteria(Department.class);
|
18 |
|
19 |
return c.list();
|
20 |
}
|
21 |
|
22 |
public static Department getDepartmentById(int id) {
|
23 |
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
|
24 |
Transaction tx = session.beginTransaction();
|
25 |
Criteria c = session.createCriteria(Department.class);
|
26 |
c.add( Restrictions.eq("id", id));
|
27 |
return (Department) c.uniqueResult();
|
28 |
|
29 |
}
|
30 |
}
|