Java tutorial
/** * Copyright (c) 2012-2014 http://www.eryansky.com * * Licensed under the Apache License, Version 2.0 (the "License"); */ package com.eryansky.entity.base; import java.io.Serializable; import java.util.List; import javax.persistence.*; import com.eryansky.core.security.SecurityConstants; import com.eryansky.entity.base.state.SexType; import com.eryansky.utils.AppConstants; import org.apache.commons.lang3.builder.ToStringBuilder; import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import com.eryansky.common.orm.PropertyType; import com.eryansky.common.orm.annotation.Delete; import com.eryansky.common.orm.entity.BaseEntity; import com.eryansky.common.utils.ConvertUtils; import com.eryansky.common.utils.collections.Collections3; import com.eryansky.utils.CacheConstants; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.google.common.collect.Lists; /** * ?User. * @author &Eryan eryanwcp@gmail.com * @date 2013-3-21 ?12:28:04 * */ @SuppressWarnings("serial") @Entity @Table(name = "T_BASE_USER") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheConstants.HIBERNATE_CACHE_BASE) //jackson??json @JsonIgnoreProperties(value = { "hibernateLazyInitializer", "handler", "fieldHandler", "resources", "roles", "defaultOrgan", "organs" }) // propertyName:?? value:"1" type: @Delete(propertyName = "status", type = PropertyType.I) public class User extends BaseEntity implements Serializable { /** * */ private String loginName; /** * ? */ private String password; /** * ?? */ private String name; /** * (0) (1) ?(2) ? */ private Integer sex = SexType.secrecy.getValue(); /** * "," */ private String email; /** * ?? */ private String address; /** * ?? "," */ private String tel; /** * ? "," */ private String mobilephone; /** * ??? */ private List<Role> roles = Lists.newArrayList(); /** * ??Roleid? */ private List<Long> roleIds = Lists.newArrayList(); /** * ? ??? */ private List<Resource> resources = Lists.newArrayList(); /** * ? id? @Transient */ private List<Long> resourceIds = Lists.newArrayList(); /** * */ private Organ defaultOrgan; /** * @Transient */ private Long defaultOrganId; /** * */ private List<Organ> organs = Lists.newArrayList(); /** * ID? @Transient */ private List<Long> organIds = Lists.newArrayList(); /** * ?? @Transient */ private String organNames; public User() { } @Column(name = "LOGIN_NAME", length = 36, nullable = false, unique = true) public String getLoginName() { return loginName; } public void setLoginName(String loginName) { this.loginName = loginName; } @Column(name = "NAME", length = 36) public String getName() { return name; } public void setName(String name) { this.name = name; } // @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY) // ,???? @JoinTable(name = "T_BASE_USER_ROLE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ROLE_ID") }) // Fecth // @Fetch(FetchMode.SUBSELECT) // ?id?. @OrderBy("id") @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheConstants.HIBERNATE_CACHE_BASE) public List<Role> getRoles() { return roles; } public void setRoles(List<Role> roles) { this.roles = roles; } @Column(name = "PASSWORD", length = 64, nullable = false) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(name = "SEX") public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } /** * ??. */ @Transient public String getSexDesc() { SexType ss = SexType.getSexType(sex); String str = ""; if (ss != null) { str = ss.getDescription(); } return str; } @Column(name = "EMAIL", length = 64) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(name = "ADDRESS", length = 255) public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Column(name = "TEL", length = 36) public String getTel() { return tel; } public void setTel(String tel) { this.tel = tel; } @Column(name = "MOBILEPHONE", length = 36) public String getMobilephone() { return mobilephone; } public void setMobilephone(String mobilephone) { this.mobilephone = mobilephone; } /** * ??, ??','. * <br>? "?" AppConstants.ROLE_SUPERADMIN */ @Transient // ??. public String getRoleNames() { Long superId = 1L; if (superId.equals(this.getId())) { return SecurityConstants.ROLE_SUPERADMIN; } return ConvertUtils.convertElementPropertyToString(roles, "name", ", "); } @SuppressWarnings("unchecked") @Transient public List<Long> getRoleIds() { if (!Collections3.isEmpty(roles)) { roleIds = ConvertUtils.convertElementPropertyToList(roles, "id"); } return roleIds; } public void setRoleIds(List<Long> roleIds) { this.roleIds = roleIds; } @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY) @JoinTable(name = "T_BASE_USER_RESOURCE", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "RESOURCE_ID") }) // @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE,region = CacheConstants.HIBERNATE_CACHE_BASE) public List<Resource> getResources() { return resources; } public void setResources(List<Resource> resources) { this.resources = resources; } @Transient public List<Long> getResourceIds() { if (!Collections3.isEmpty(resources)) { resourceIds = ConvertUtils.convertElementPropertyToList(resources, "id"); } return resourceIds; } public void setResourceIds(List<Long> resourceIds) { this.resourceIds = resourceIds; } @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "DEFAULT_ORGANID") public Organ getDefaultOrgan() { return defaultOrgan; } public void setDefaultOrgan(Organ defaultOrgan) { this.defaultOrgan = defaultOrgan; } @Transient public Long getDefaultOrganId() { if (defaultOrgan != null) { defaultOrganId = defaultOrgan.getId(); } return defaultOrganId; } public void setDefaultOrganId(Long defaultOrganId) { this.defaultOrganId = defaultOrganId; } @Transient public String getDefaultOrganName() { String doName = null; if (defaultOrgan != null) { doName = defaultOrgan.getName(); } return doName; } @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.LAZY) @JoinTable(name = "T_BASE_USER_ORGAN", joinColumns = { @JoinColumn(name = "USER_ID") }, inverseJoinColumns = { @JoinColumn(name = "ORGAN_ID") }) @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE, region = CacheConstants.HIBERNATE_CACHE_BASE) public List<Organ> getOrgans() { return organs; } public void setOrgans(List<Organ> organs) { this.organs = organs; } @Transient public List<Long> getOrganIds() { if (!Collections3.isEmpty(organs)) { organIds = ConvertUtils.convertElementPropertyToList(organs, "id"); } return organIds; } public void setOrganIds(List<Long> organIds) { this.organIds = organIds; } @Transient public String getOrganNames() { return ConvertUtils.convertElementPropertyToString(organs, "name", ", "); } }