io.robe.admin.hibernate.dao.RoleDao Maven / Gradle / Ivy
package io.robe.admin.hibernate.dao;
import com.google.common.base.Optional;
import io.robe.admin.hibernate.entity.Role;
import io.robe.auth.data.entry.RoleEntry;
import io.robe.auth.data.store.RoleStore;
import io.robe.hibernate.dao.BaseDao;
import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import javax.inject.Inject;
public class RoleDao extends BaseDao implements RoleStore {
@Inject
public RoleDao(SessionFactory sessionFactory) {
super(sessionFactory);
}
public Optional extends RoleEntry> findByRoleId(String oid) {
return Optional.fromNullable(findById(oid));
}
public Optional findByName(String code) {
Criteria criteria = currentSession().createCriteria(Role.class);
criteria.add(Restrictions.eq("code", code));
return Optional.fromNullable(uniqueResult(criteria));
}
public Optional findByNameAndNotEqualMe(String code, String oid) {
Criteria criteria = currentSession().createCriteria(Role.class);
criteria.add(Restrictions.eq("code", code));
criteria.add(Restrictions.not(Restrictions.eq("oid", oid)));
return Optional.fromNullable(uniqueResult(criteria));
}
}