⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rolehibernate.java

📁 java实现的可配置的工作流引擎,采用jsp+javabean实现
💻 JAVA
字号:
package com.hongsoft.res.database;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;

import com.hongsoft.res.pojo.ResException;
import com.hongsoft.res.pojo.ResObject;
import com.hongsoft.res.pojo.ResType;
import com.hongsoft.res.pojo.Role;
import com.hongsoft.res.util.IDGenerator;

public class RoleHibernate {
    private static RoleHibernate dao = null;

    static RoleHibernate getInstance() {
        return new RoleHibernate();
    }

    private RoleHibernate() {

    }

    /**
     * 创建ROLE,其中角色名不能重复
     *
     * @param role
     * @return 角色ID
     * @throws ResException
     */
    public synchronized Role createRole(Session session, Role role) throws ResException {
        int errorCode = ResException.ROLE_NAME_UNIQUE_ERROR;
        long value = 0;
        try {
            List role2 = null;
            // session.find(ResQuery.LOAD_ROLE_BY_NAME,role.getName(),new StringType());
            if (role2 == null || role2.isEmpty()) {
                errorCode = ResException.OTHER_DATABASE_ERROR;
                value = IDGenerator.newID(session, ResObjectName.RES_OBJECT);
                ResObject resObject = new ResObject();
                resObject.setId(value);
                resObject.setType(ResType.ROLE);
                role.setId(value);
                resObject.setRole(role);
                session.save(resObject);
            } else {
                throw new ResException(errorCode);
            }
        } catch (HibernateException e) {
            throw new ResException(errorCode);
        }
        return role;
    }

    /**
     * 从数据库获取角色信息对象
     *
     * @param id 角色对应的资源ID
     * @return Role,如果搜索不到返回null
     */
    public Role getRole(Session session, long id) throws ResException {
        int errorCode = ResException.OTHER_DATABASE_ERROR;
        Role role = null;
        try {
            List roleList = null;
            // session.find(ResQuery.LOAD_ROLE_BY_ID, new Long(id), new LongType());
            Iterator itr = roleList.iterator();
            if (itr.hasNext())
                role = (Role) itr.next();
        } catch (HibernateException e) {
            throw new ResException(errorCode);
        }
        return role;

    }

    /**
     * 从数据库获取角色信息对象集合
     *
     * @param name 角色名称
     * @param isLike 查询关键字是LIKE还是=
     * @return 角色对象(Role)集合,如果搜索不到返回null
     */
    public List searchRole(Session session, String name, boolean isLike) throws ResException {
        int errorCode = ResException.OTHER_DATABASE_ERROR;
        List roleList = new ArrayList();
        try {
            String searchQuery = "SELECT r FROM Role as r WHERE r.name like :name";
            Query q = session.createQuery(searchQuery);
            if (isLike)
                q.setString("name", '%' + name + '%');
            else
                q.setString("name", name);
            roleList = q.list();
        } catch (HibernateException e) {
            e.printStackTrace();
            throw new ResException(errorCode);
        }
        return roleList;
    }

    /**
     * 修改数据库角色信息
     *
     * @param role Role
     * @throws ResException
     */
    public void modifyRole(Session session, Role role) throws ResException {
        try {
            session.update(role);
        } catch (HibernateException e) {
            throw new ResException(ResException.OTHER_DATABASE_ERROR);
        }
    }

    /**
     * 从数据库删除角色信息
     *
     * @param id 角色对应的资源ID
     */
    public void deleteRole(Session session, long id) throws ResException {
        try {
            // session.delete(ResQuery.LOAD_ROLE_BY_ID_FROM_RESOBJECT, new Long(id), new LongType());
        } catch (HibernateException e) {
            throw new ResException(ResException.RES_NOT_FOUND_ERROR);
        }
    }
}

⌨️ 快捷键说明

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