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;
}
相关推荐
这些注解,如`@Entity`表示该类是一个实体,`@Table`定义了实体对应的数据库表,`@Id`标识主键字段,`@Column`描述了属性与数据库列的映射关系等,它们大大简化了代码编写过程。 在生成实体类之后,你可以在项目的...
在Java Persistence API (JPA) 中,EclipseLink作为一个强大的对象关系映射(ORM)框架,允许开发者将数据库中的数据模型映射到Java对象。在处理特定数据库特性时,如Oracle的SDO_GEOMETRY(Spatial Data Object ...
JPA还支持关联管理,包括一对一(@OneToOne)、一对多(@OneToMany)、多对一(@ManyToOne)和多对多(@ManyToMany)的关系。关联可以在实体类中通过`@JoinTable`、`@JoinColumn`等注解来配置。 事务管理也是JPA的...
1. **Java Persistence API (JPA)**:JPA是Java平台上的一个标准,用于对象关系映射(ORM),允许开发者用Java对象来操作数据库记录,而无需编写SQL语句。JPA提供了一种规范,使得开发者能够更专注于业务逻辑,而不是...
随着JPA标准的推广和被广泛采用,越来越多的厂商和框架开始支持JPA,其中包括Spring Framework、OpenJPA、EclipseLink(原TopLink)等。这种广泛的采纳使得JPA成为了一个重要的行业标准,为开发者提供了丰富的资源和...
JPA是Java中的对象关系映射(ORM)标准,它允许我们在Java对象和数据库表之间建立映射,从而避免了直接编写SQL语句。通过使用JPA,我们可以使用Java方法来执行CRUD操作,而不是传统的SQL语句。 首先,我们需要在...
Eclipse JPA是Java EE平台中一个重要的组件,用于对象关系映射(ORM),它简化了数据库操作,使开发人员可以使用面向对象的方式来操作数据库。 一、Eclipse JPA简介 Eclipse JPA是一个开源的JPA实现,由Eclipse基金...
Java 持久化 API(JPA)是 Java 平台上的一种标准化的对象关系映射(ORM)技术,用于实现 Java 应用程序中的持久化操作。JPA 元模型是根据实体类动态生成的一个类,它的主要作用是实现 JPA criteria 查询的“类型...
在Java世界中,Java Persistence API(JPA)是用于管理和持久化对象的规范,它使得开发者可以方便地在关系数据库和面向对象模型之间进行映射。JPA的主要目标是简化数据库操作,使得开发者能更专注于业务逻辑而不是...
本文将深入探讨JPA中的四种主要关联关系:一对一(OneToOne)、一对多(OneToMany)、多对一(ManyToOne)和多对多(ManyToMany)。 1. **一对一关联(OneToOne)** 在一对一关联中,一个实体实例对应数据库中的唯...
除了这两者,还有其他一些ORM解决方案,如JPA(Java Persistence API),它是Java EE的一部分,提供了一套标准的ORM接口,而实现JPA规范的库有EclipseLink、OpenJPA等。 学习将对象映射到关系数据库的关键在于理解...
在IT行业中,数据库操作是应用程序的核心部分,Spring JPA(Java Persistence API)是Spring框架提供的一种简化ORM(对象关系映射)操作的方式。本主题主要围绕"spring jpa操作数据库 级联数据 hibernate"展开,探讨...
它提供了一种对象关系映射方法,允许您以标准、可移植的方式声明性地定义如何将Java对象映射到关系数据库表,不仅在Java EE 5应用服务器内部,而且在Java标准版(Java SE)5应用程序外部都能工作。 在使用TopLink ...
**Java Persistence API (JPA)** 是Java平台上的一个标准,用于管理关系数据库中的数据,它为开发者提供了一种对象关系映射(ORM)框架,使得开发者可以使用面向对象的方式来处理数据库交互,而无需直接编写SQL语句...
JPA-eclipselink-项目使用 EclipseLink 实现的 JPA 项目示例JPA:java persistence api 是一个 O/R 映射持久化的规范。 API 描述了如何将 Java 对象映射到关系数据库。 此映射确保将对象转换为数据库,反之亦然,...
实体之间的关系是数据模型的重要组成部分,JPA提供了多种关系映射方式,包括一对一、一对多、多对一和多对多。通过`@OneToOne`、`@OneToMany`、`@ManyToOne`和`@ManyToMany`等注解,可以灵活地在实体类中表示各种...
3. Hibernate:这是一个ORM(对象关系映射)工具,使得Java程序可以方便地操作数据库。 而JPA是Java标准API,用于管理和持久化Java对象到关系数据库,它简化了数据库操作,通过提供一套对象模型和API,避免了直接...
JPA 2.0引入了许多增强功能,比如支持@OneToMany和@ManyToMany关系的双向映射,新增了@Temporal注解以处理日期和时间,增强了Criteria API,引入了@SecondaryTable注解以支持多表映射,还引入了Entity Graph来优化...
此外,随着技术的发展,许多现代ORM框架如JPA和Spring Data JPA提供了更高级的抽象,可以完全摆脱XML映射,转而使用注解来描述对象关系映射。 总的来说,"Middlegen-Hibernate-r5"是面向Hibernate用户的一款实用...
**Java Persistence API (JPA)** 是一种用于处理数据库操作的标准接口,为Java开发人员提供了一种对象关系映射(ORM)机制。JPA2.0作为JPA的一个重大版本更新,在原有基础上增加了许多新功能和改进,使得数据持久化...