RoleDaoHibernate.java
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.myproject.dao.hibernate;
import com.myproject.dao.RoleDao;
import com.myproject.model.Role;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import java.util.List;
/**
* This class interacts with hibernate session to save/delete and
* retrieve Role objects.
*
* @author <a href="mailto:bwnoll@gmail.com">Bryan Noll</a>
* @author jgarcia (updated to hibernate 4)
*/
@Repository
public class RoleDaoHibernate extends GenericDaoHibernate<Role, Long> implements RoleDao {
/**
* Constructor to create a Generics-based version using Role as the entity
*/
public RoleDaoHibernate() {
super(Role.class);
}
/**
* {@inheritDoc}
*/
public Role getRoleByName(String rolename) {
List roles = getSession().createCriteria(Role.class).add(Restrictions.eq("name", rolename)).list();
if (roles.isEmpty()) {
return null;
} else {
return (Role) roles.get(0);
}
}
/**
* {@inheritDoc}
*/
public void removeRole(String rolename) {
Object role = getRoleByName(rolename);
Session session = getSessionFactory().getCurrentSession();
session.delete(role);
}
}