eclipsejpa多对多关系映射

 

 

1,账户端的model类

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;



/**
 *
 * @author
 *
 */
@Entity
@Table(name = "SManagerTab")
public class AccountInfo implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = 6479111553009031395L;

    /**
     * 帐号id
     */
    @Id()
    @GeneratedValue(strategy =GenerationType.IDENTITY)
    private Integer mgrId;

    /**
     * 帐号名称
     */
    @Basic(optional = false)
    private String mgrName;

    /**
     * 登录名称
     */
    private String mgrNickName;

    /**
     * 登录密码
     */
    @Column(name = "mgrPasswd    ")
    private String mgrPassword = "888888";

    /**
     * 注册时间
     */
    @Column(name = "registerTime")
    @Temporal(TemporalType.TIMESTAMP)
    private Date registerTime = new Date();

 

   //此为多对多的关系映射,
    @ManyToMany(cascade = { CascadeType.REFRESH})

//SManagerRoleTab为数据库中间表的表名

//joinColumn 为账户中实体的主键字段,inverseJoinColumns为角色实体中的主键字段
    @JoinTable(name = "SManagerRoleTab", joinColumns = { @JoinColumn(name = "mgrId") }, inverseJoinColumns = { @JoinColumn(name = "roleName") })
    private Set<RoleInfo> roleInfos = new HashSet<RoleInfo>();
   
    @ManyToMany(cascade = {CascadeType.REFRESH},fetch=FetchType.LAZY)
    @JoinTable(name = "SManagerResTab", joinColumns = { @JoinColumn(name = "mgrId") }, inverseJoinColumns = { @JoinColumn(name = "resName") })
    private Set<ResourcesInfo> resourcesInfos=new HashSet<ResourcesInfo>();
   
    /**
     * 邮箱
     */
    private String mgrEmail;

    /**
     * 登录次数
     */
    private Integer loginCount = new Integer(0);

    /**
     * 最后一次登录IP地址
     */
    private String lastLoginAddr = "127,0,0,1";

    /**
     * 最后一次登录的时间
     */
    @Basic
    @Temporal(TemporalType.TIMESTAMP)
    private Date lastLoginTime = new Date();

    /**
     * 当前账户的状态
     */
    private Integer mgrStatus = new Integer(UserStatus.INCOMPLETE.getStatus());

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY,mappedBy="accountInfo")
    private Set<AccountAttrInfo> accountAttrInfos = new HashSet<AccountAttrInfo>();

    /**
     * 手机号
     */
    @Transient
    private String mobliePhone;

    /**
     * 办公电话
     */
    @Transient
    private String telephone;

    /**
     * 部门
     */
    @Transient
    private String department;

    /**
     * 单位
     */
    @Transient
    private String company;

    /**
     * 职务
     */
    @Transient
    private String post;
    @Transient
    private String roleNames="";
    @Transient
    private String agentGroupNames="";
    @Transient
    private String queueNames="";
    @Transient
    private String groupType="";
    public AccountInfo() {
    }

    public AccountInfo(Integer mgrId, String mgrName, String mgrNickName,
            String mgrPassword, Date registerTime, Set<RoleInfo> roleInfos,
            String mgrEmail, Integer loginCount, String lastLoginAddr,
            Date lastLoginTime, Integer mgrStatus) {
        super();
        this.mgrId = mgrId;
        this.mgrName = mgrName;
        this.mgrNickName = mgrNickName;
        this.mgrPassword = mgrPassword;
        this.registerTime = registerTime;
        this.roleInfos = roleInfos;
        this.mgrEmail = mgrEmail;
        this.loginCount = loginCount;
        this.lastLoginAddr = lastLoginAddr;
        this.lastLoginTime = lastLoginTime;
        this.mgrStatus = mgrStatus;
    }

    public Integer getMgrId() {
        return mgrId;
    }

    public void setMgrId(Integer mgrId) {
        this.mgrId = mgrId;
    }

    public String getMgrName() {
        return mgrName;
    }

    public void setMgrName(String mgrName) {
        this.mgrName = mgrName;
    }

    public String getMgrNickName() {
        return mgrNickName;
    }

    public void setMgrNickName(String mgrNickName) {
        this.mgrNickName = mgrNickName;
    }

    public String getMgrPassword() {
        return mgrPassword;
    }

    public void setMgrPassword(String mgrPassword) {
        this.mgrPassword = mgrPassword;
    }

    public Date getRegisterTime() {
        return registerTime;
    }

    public void setRegisterTime(Date registerTime) {
        this.registerTime = registerTime;
    }

    public String getMgrEmail() {
        return mgrEmail;
    }

    public void setMgrEmail(String mgrEmail) {
        this.mgrEmail = mgrEmail;
    }

    public Integer getLoginCount() {
        return loginCount;
    }

    public void setLoginCount(Integer loginCount) {
        this.loginCount = loginCount;
    }

    public String getLastLoginAddr() {
        return lastLoginAddr;
    }

    public void setLastLoginAddr(String lastLoginAddr) {
        this.lastLoginAddr = lastLoginAddr;
    }

    public Date getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Date lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public Integer getMgrStatus() {
        return mgrStatus;
    }

    public void setMgrStatus(Integer mgrStatus) {
        this.mgrStatus = mgrStatus;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        AccountInfo other = (AccountInfo) obj;
        if (mgrId == null) {
            if (other.mgrId != null)
                return false;
        } else if (!mgrId.equals(other.mgrId))
            return false;
        if (mgrName == null) {
            if (other.mgrName != null)
                return false;
        } else if (!mgrName.equals(other.mgrName))
            return false;
        return true;
    }

    public Set<RoleInfo> getRoleInfos() {
        roleNames="";
        if(roleInfos.size()>0){
            for(RoleInfo roleInfo:roleInfos){
                roleNames+=roleInfo.getRoleName()+",";
                //去掉末尾的,号
            }
            //roleNames=roleNames.substring(roleNames.length()-1);
        }
        return roleInfos;
    }

    public void setRoleInfos(Set<RoleInfo> roleInfos) {
        this.roleInfos = roleInfos;
    }

    public Set<AccountAttrInfo> getAccountAttrInfos() {
        if(accountAttrInfos.size()>0){
            for (AccountAttrInfo accountAttrInfo : accountAttrInfos) {
                if("company".equals(accountAttrInfo.getAttrName())){
                    this.company=accountAttrInfo.getAttrValue();
                }else if("department".equals(accountAttrInfo.getAttrName())){
                    this.department=accountAttrInfo.getAttrValue();
                }else if("mobliePhone".equals(accountAttrInfo.getAttrName())){
                    this.mobliePhone=accountAttrInfo.getAttrValue();
                }else if("post".equals(accountAttrInfo.getAttrName())){
                    this.post=accountAttrInfo.getAttrValue();
                }else if("telephone".equals(accountAttrInfo.getAttrName())){
                    this.telephone=accountAttrInfo.getAttrValue();
                }
            }
        }
        return accountAttrInfos;
    }

    public void setAccountAttrInfos(Set<AccountAttrInfo> accountAttrInfos) {
        this.accountAttrInfos = accountAttrInfos;
    }

    public String getMobliePhone() {
        return mobliePhone;
    }

    public void setMobliePhone(String mobliePhone) {
        this.mobliePhone = mobliePhone;
    }

    public String getTelephone() {
        return telephone;
    }

    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }

    public String getDepartment() {
        return department;
    }

    public void setDepartment(String department) {
        this.department = department;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public void getAccountAttrs() {
    }

    public String getRoleNames() {
        return roleNames;
    }

    public void setRoleNames(String roleNames) {
        this.roleNames = roleNames;
    }

    public Set<ResourcesInfo> getResourcesInfos() {
        //座席组
        agentGroupNames="";
        if(resourcesInfos.size()>0){
            for (ResourcesInfo resourcesInfo : resourcesInfos) {
                if("AgentGroup".equals(resourcesInfo.getResType())){
                    agentGroupNames += resourcesInfo.getResName()+",";
                }else if("Queue".equals(resourcesInfo.getResType())){
                    queueNames+=resourcesInfo.getResName() + ",";
                }
            }
        }
        return resourcesInfos;
    }

    public void setResourcesInfos(Set<ResourcesInfo> resourcesInfos) {
        this.resourcesInfos = resourcesInfos;
    }

    public String getGroupType() {
        return groupType;
    }

    public void setGroupType(String groupType) {
        this.groupType = groupType;
    }

    @Override
    public String toString() {
        return "AccountInfo [mgrId=" + mgrId + ", mgrName=" + mgrName
                + ", mgrNickName=" + mgrNickName + ", mgrPassword="
                + mgrPassword + ", registerTime=" + registerTime
                + ", roleInfos=" + roleInfos + ", resourcesInfos="
                + resourcesInfos + ", mgrEmail=" + mgrEmail + ", loginCount="
                + loginCount + ", lastLoginAddr=" + lastLoginAddr
                + ", lastLoginTime=" + lastLoginTime + ", mgrStatus="
                + mgrStatus + ", accountAttrInfos=" + accountAttrInfos
                + ", mobliePhone=" + mobliePhone + ", telephone=" + telephone
                + ", department=" + department + ", company=" + company
                + ", post=" + post + ", roleNames=" + roleNames
                + ", agentGroupNames=" + agentGroupNames + ", groupType=" + groupType
                + ", queueNames=" + queueNames
                + "]";
    }

    public String getAgentGroupNames() {
        return agentGroupNames;
    }

    public void setAgentGroupNames(String agentGroupNames) {
        this.agentGroupNames = agentGroupNames;
    }

    public String getQueueNames() {
        return queueNames;
    }

    public void setQueueNames(String queueNames) {
        this.queueNames = queueNames;
    }
   
}
2.角色model类



import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

@Entity
@Table(name="SRoleTab")
public class RoleInfo implements Serializable{

    /**
     *
     */
    private static final long serialVersionUID = -3848485082265308899L;

    /**
     * 角色名称
     */
    @Id
    @GeneratedValue
    private String roleName;

    /**
     * 角色
     */
    private String roleDescr;

    /**
     * 类型
     */
    private Integer isInLine=new Integer(0);
//此处为对应的账户配置信息,mappedBy为交给账户端管理,即双方配置关系时,交由一方管理,加载方式最好为懒加载,需要用的时候调用get方法即可
    @ManyToMany(cascade = {CascadeType.PERSIST}, mappedBy = "roleInfos", fetch = FetchType.LAZY)
    private Set<AccountInfo> accountInfos=new HashSet<AccountInfo>();
   
    @ManyToMany(cascade = { CascadeType.PERSIST,CascadeType.REFRESH},fetch=FetchType.LAZY)
    @JoinTable(name = "SRolePermTab", joinColumns = { @JoinColumn(name = "roleName") }, inverseJoinColumns = { @JoinColumn(name = "permKey") })
    private Set<PermissionInfo> permissionInfos=new HashSet<PermissionInfo>();
    @Transient
    private String permission;
   
    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }


    public String getRoleDescr() {
        return roleDescr;
    }

    public void setRoleDescr(String roleDescr) {
        this.roleDescr = roleDescr;
    }

   

    public Integer getIsInLine() {
        return isInLine==null ? new Integer(0):isInLine;
    }

    public void setIsInLine(Integer isInLine) {
        this.isInLine = isInLine;
    }

    public RoleInfo() {
    }

   

    public RoleInfo(String roleName, String roleDescr, Integer isInLine) {
        super();
        this.roleName = roleName;
        this.roleDescr = roleDescr;
        this.isInLine = isInLine;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result
                + ((roleDescr == null) ? 0 : roleDescr.hashCode());
        result = prime * result
                + ((roleName == null) ? 0 : roleName.hashCode());
        result = prime * result + ((isInLine == null) ? 0 : isInLine.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        RoleInfo other = (RoleInfo) obj;
        if (roleName == null) {
            if (other.roleName != null)
                return false;
        } else if (!roleName.equals(other.roleName))
            return false;
        return true;
    }

    public Set<AccountInfo> getAccountInfos() {
        return accountInfos;
    }

    public void setAccountInfos(Set<AccountInfo> accountInfos) {
        this.accountInfos = accountInfos;
    }

    public Set<PermissionInfo> getPermissionInfos() {
        permission="";
        if(permissionInfos.size()>0){
            for (PermissionInfo permissionInfo :permissionInfos) {
                permission += permissionInfo.getPermKey()+",";
            }
        }
        return permissionInfos;
    }

    public void setPermissionInfos(Set<PermissionInfo> permissionInfos) {
        this.permissionInfos = permissionInfos;
    }

    public String getPermission() {
        return permission;
    }

    public void setPermission(String permission) {
        this.permission = permission;
    }

    @Override
    public String toString() {
        return "RoleInfo [roleName=" + roleName + ", roleDescr=" + roleDescr
                + ", isInLine=" + isInLine + ", accountInfos=" + accountInfos
                + ", permissionInfos=" + permissionInfos + ", permission="
                + permission + "]";
    }
   
   
}
3.保存时可按如下方法即可

/**
     * 添加用户
     *
     * @return
     * @throws IOException
     */
    public String addAccountinfo() throws IOException {
        String result = "";
        try {
            this.roleNames = accountInfo.getRoleNames();
            // 角色信息
            if (!"".equals(roleNames)) {
                String[] roles = roleNames.split(",");
                Set<RoleInfo> roleInfos = new HashSet<RoleInfo>();
                for (int i = 0; i < roles.length; i++) {
                    RoleInfo role = null;
                    Query query = this.accountInfoService
                            .getEmEntityManager()
                            .createQuery(
                                    "select r from RoleInfo r where r.roleName=:roleName ");
                    query.setParameter("roleName", roles[i]);
                    role = (RoleInfo) query.getSingleResult();
                    roleInfos.add(role);
                    role.setPermissionInfos(null);
                }
                this.accountInfo.setRoleInfos(roleInfos);
            }
            // 管辖范围 座席
            Set<ResourcesInfo> resourcesInfos = new HashSet<ResourcesInfo>();
            try {
                // 坐席组
                if (!"".equals(accountInfo.getAgentGroupNames())) {
                    String[] groups = accountInfo.getAgentGroupNames().split(
                            ",");
                    for (int i = 0; i < groups.length; i++) {
                        String res = groups[i];
                        String[] s = res.split("@");
                        // 查询resources表中有没有该坐席组
                        ResourcesInfo resourcesInfo = this
                                .getAccountInfoService().queryByname(s[0]);
                        if (resourcesInfo == null) {
                            AgentGroupInfo group = new AgentGroupInfo();
                            group = this.getAccountInfoService()
                                    .queryAgentByName(s[0]);
                            if (group != null) {
                                resourcesInfo = new ResourcesInfo();
                                resourcesInfo.setResName(group.getName());
                                resourcesInfo.setResType(Constants.AGENTGROUP);
                                resourcesInfo.setResClazz(group
                                        .getAgentGroupId());
                                byte[] a = new byte[2];
                                resourcesInfo.setResClazzParams(a);
                            }

                        }

                        resourcesInfos.add(resourcesInfo);
                    }
                }
                // 技能组
                if (!"".equals(accountInfo.getQueueNames())) {
                    String[] groups = accountInfo.getQueueNames().split(",");
                    for (int i = 0; i < groups.length; i++) {
                        String res = groups[i];
                        String[] s = res.split("@");
                        ResourcesInfo resourcesInfo = this
                                .getAccountInfoService().queryByname(s[0]);
                        if (resourcesInfo == null) {
                            QueueInfo q = new QueueInfo();
                            q = this.getAccountInfoService().queryQueueByName(
                                    s[0]);
                            if (q != null) {
                                resourcesInfo = new ResourcesInfo();
                                resourcesInfo.setResName(q.getName());
                                resourcesInfo.setResType(Constants.QUEUE);
                                resourcesInfo.setResClazz(q.getSkilldbId()
                                        .toString());
                                byte[] a = new byte[2];
                                resourcesInfo.setResClazzParams(a);
                            }

                        }

                        resourcesInfos.add(resourcesInfo);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                resourcesInfos.add(new ResourcesInfo());
            }

            this.accountInfo.setResourcesInfos(resourcesInfos);
            this.accountInfoService.getAccountInfoRepository().save(
                    this.accountInfo);
            this.accountInfo.getMgrId();
            // 设置电话,单位,部门,等信息
            this.accountInfo.setAccountAttrInfos(getSManagerAttrValeus());
            this.accountInfoService.getAccountInfoRepository().save(
                    this.accountInfo);
            this.accountInfo = new AccountInfo();
            // 重定向
            result = "accountInfoList.xhtml";
            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect(result);

        } catch (Exception e) {
            e.printStackTrace();
            this.flag = 1;
            result = "addAccountInfo.xhtml?f=" + 1;
            FacesContext.getCurrentInstance().getExternalContext().redirect(result);
        }
        return result;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值