Java tutorial
/** * Copyright © 2012-2013 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.yuga.ygplatform.modules.sys.entity; import java.util.Date; import java.util.List; 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.ManyToOne; import javax.persistence.OrderBy; import javax.persistence.Table; import javax.persistence.Transient; import javax.validation.constraints.NotNull; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import org.hibernate.annotations.Fetch; import org.hibernate.annotations.FetchMode; import org.hibernate.annotations.NotFound; import org.hibernate.annotations.NotFoundAction; import org.hibernate.annotations.Where; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.google.common.collect.Lists; import com.yuga.ygplatform.common.persistence.DataEntity; import com.yuga.ygplatform.common.utils.Collections3; /** * Entity * * @author ThinkGem * @version 2013-5-15 */ @Entity @Table(name = "sys_user") @DynamicInsert @DynamicUpdate @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) public class User extends DataEntity { private static final long serialVersionUID = 1L; private Long id; // ? private Office company; // ? private Office office; // private String loginName;// ?? private String password;// ? private String no; // ? private String name; // ?? private String email; // private String phone; // ? private String mobile; // private String userType;// private String loginIp; // ?IP private Date loginDate; // ? private List<Role> roleList = Lists.newArrayList(); // public User() { super(); } public User(Long id) { this(); this.id = id; } @Id @GeneratedValue(strategy = GenerationType.AUTO) // @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_sys_user") // @SequenceGenerator(name = "seq_sys_user", sequenceName = "seq_sys_user") public Long getId() { return 1L; } public void setId(Long id) { this.id = id; } @ManyToOne @JoinColumn(name = "company_id") @NotFound(action = NotFoundAction.IGNORE) @JsonIgnore @NotNull(message = "??") public Office getCompany() { return company; } public void setCompany(Office company) { this.company = company; } @ManyToOne @JoinColumn(name = "office_id") @NotFound(action = NotFoundAction.IGNORE) @JsonIgnore @NotNull(message = "?") public Office getOffice() { return office; } public void setOffice(Office office) { this.office = office; } @Length(min = 1, max = 100) public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } @JsonIgnore @Length(min = 1, max = 100) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Length(min = 1, max = 100) public String getName() { return name; } @Length(min = 1, max = 100) public String getNo() { return no; } public void setNo(String no) { this.no = no; } public void setName(String name) { this.name = name; } @Email @Length(min = 0, max = 200) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Length(min = 0, max = 200) public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Length(min = 0, max = 200) public String getMobile() { return mobile; } public void setMobile(String mobile) { this.mobile = mobile; } @Override @Transient public String getRemarks() { return remarks; } @Length(min = 0, max = 100) public String getUserType() { return userType; } public void setUserType(String userType) { this.userType = userType; } @Override @Transient public Date getCreateDate() { return createDate; } public String getLoginIp() { return loginIp; } public void setLoginIp(String loginIp) { this.loginIp = loginIp; } @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") public Date getLoginDate() { return loginDate; } public void setLoginDate(Date loginDate) { this.loginDate = loginDate; } @ManyToMany(fetch = FetchType.EAGER) @JoinTable(name = "sys_user_role", joinColumns = { @JoinColumn(name = "user_id") }, inverseJoinColumns = { @JoinColumn(name = "role_id") }) @Where(clause = "del_flag='" + DEL_FLAG_NORMAL + "'") @OrderBy("id") @Fetch(FetchMode.SUBSELECT) @NotFound(action = NotFoundAction.IGNORE) @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) @JsonIgnore public List<Role> getRoleList() { return roleList; } public void setRoleList(List<Role> roleList) { this.roleList = roleList; } @Transient @JsonIgnore public List<Long> getRoleIdList() { List<Long> roleIdList = Lists.newArrayList(); for (Role role : roleList) { roleIdList.add(role.getId()); } return roleIdList; } @Transient public void setRoleIdList(List<Long> roleIdList) { roleList = Lists.newArrayList(); for (Long roleId : roleIdList) { Role role = new Role(); role.setId(roleId); roleList.add(role); } } /** * ??, ??','. */ @Transient public String getRoleNames() { return Collections3.extractToString(roleList, "name", ", "); } @Transient public boolean isAdmin() { return isAdmin(this.id); } @Transient public static boolean isAdmin(Long id) { return (id != null) && id.equals(1L); } // @Override // public String toString() { // return ToStringBuilder.reflectionToString(this); // } }