rolefunctiondao.java

来自「struts2 spring2.5 jpa hibernate 权限管理系统源代」· Java 代码 · 共 105 行

JAVA
105
字号
package sunyang.relationship.dao;

import java.util.List;
import javax.persistence.*;

import sunyang.functions.domain.Function;
import sunyang.module.domain.Module;
import sunyang.persistence.EntityManagerHelper;
import sunyang.relationship.domain.RoleFunction;
import sunyang.role.domain.Role;

public class RoleFunctionDAO implements IRoleFunctionDAO {
	// 得到实体管理器
	private EntityManager getEntityManager() {
		return EntityManagerHelper.getEntityManager();
	}

	// 新增数据
	public void save(RoleFunction entity) {
		try {
			EntityManagerHelper.beginTransaction();
			getEntityManager().persist(entity);
			EntityManagerHelper.commit();
		} catch (RuntimeException re) {
			EntityManagerHelper.rollback();
			throw re;
		}
	}

	// 删除数据
	public void delete(RoleFunction entity) {
		try {
			EntityManagerHelper.beginTransaction();
			entity = getEntityManager().getReference(RoleFunction.class,
					entity.getId());
			getEntityManager().remove(entity);
			EntityManagerHelper.commit();
		} catch (RuntimeException re) {
			EntityManagerHelper.rollback();
			throw re;
		}
	}

	// 通过表中一个字段进行查询
	@SuppressWarnings("unchecked")
	public List<RoleFunction> findByProperty(String propertyName,
			final Object value) {
		try {
			final String queryString = "select model from RoleFunction model where model."
					+ propertyName + "= :propertyValue";
			Query query = getEntityManager().createQuery(queryString).setHint(
					"toplink.refresh", true);
			query.setParameter("propertyValue", value);
			return query.getResultList();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	// 通过表中两个字段进行查询
	@SuppressWarnings("unchecked")
	public List<RoleFunction> findBy2Properties(String propertyName1,
			String propertyName2, final Object value1, final Object value2) {
		try {
			final String queryString = "select model from RoleFunction model where model."
					+ propertyName1
					+ "= :propertyValue1 and model."
					+ propertyName2 + "=:propertyValue2";
			Query query = getEntityManager().createQuery(queryString).setHint(
					"toplink.refresh", true);
			query.setParameter("propertyValue1", value1).setParameter(
					"propertyValue2", value2);
			return query.getResultList();
		} catch (RuntimeException re) {
			throw re;
		}
	}

	// 通过角色id查询模块
	@SuppressWarnings("unchecked")
	public List<Module> findModuleHad(Integer rid) {
		String queryString = "select distinct rf.function.module from RoleFunction rf where rf.role.id=:roleid";
		return getEntityManager().createQuery(queryString).setHint(
				"toplink.refresh", true).setParameter("roleid", rid)
				.getResultList();
	}

	// 通过模块id和角色id查询功能
	@SuppressWarnings("unchecked")
	public List<Function> findFInRM(Integer rid, Module m) {
		String queryString = "select distinct rf.function from RoleFunction rf where rf.role.id=:roleid and rf.function.module.id=:mid";
		return getEntityManager().createQuery(queryString).setHint(
				"toplink.refresh", true).setParameter("roleid", rid)
				.setParameter("mid", m.getId()).getResultList();
	}

	// 通过角色id查询功能id
	@SuppressWarnings("unchecked")
	public List<Integer> findFidByRid(Role r) {
		String queryString = "select distinct rf.function.id from RoleFunction rf where rf.role.id=:roleid";
		return getEntityManager().createQuery(queryString).setHint(
				"toplink.refresh", true).setParameter("roleid", r.getId())
				.getResultList();
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?