Package org.springside.examples.miniweb.dao.account

Source Code of org.springside.examples.miniweb.dao.account.RoleDao

package org.springside.examples.miniweb.dao.account;

import java.util.List;

import org.springframework.stereotype.Component;
import org.springside.examples.miniweb.entity.account.Role;
import org.springside.examples.miniweb.entity.account.User;
import org.springside.modules.orm.hibernate.HibernateDao;

/**
* 角色对象的泛型DAO.
*
* @author calvin
*/
@Component
public class RoleDao extends HibernateDao<Role, Long> {

  private static final String QUERY_USER_BY_ROLEID = "select u from User u left join u.roleList r where r.id=?";

  /**
   * 重载函数,因为Role中没有建立与User的关联,因此需要以较低效率的方式进行删除User与Role的多对多中间表.
   */
  @SuppressWarnings("unchecked")
  @Override
  public void delete(Long id) {
    Role role = get(id);
    //查询出拥有该角色的用户,并删除该用户的角色.
    List<User> users = createQuery(QUERY_USER_BY_ROLEID, role.getId()).list();
    for (User u : users) {
      u.getRoleList().remove(role);
    }
    super.delete(role);
  }
}
TOP

Related Classes of org.springside.examples.miniweb.dao.account.RoleDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.