Java tutorial
/* * Copyright 2005-2015 shopxx.net. All rights reserved. * Support: http://3936242.01p.com/ * License: http://3936242.01p.com/license */ package net.shopxx.entity; import java.util.Date; import java.util.HashSet; import java.util.Set; import java.util.UUID; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.FetchType; import javax.persistence.JoinTable; import javax.persistence.ManyToMany; import javax.persistence.PrePersist; import javax.persistence.PreUpdate; import javax.persistence.SequenceGenerator; import javax.persistence.Table; import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang.RandomStringUtils; import org.apache.commons.lang.StringUtils; import org.hibernate.validator.constraints.Email; import org.hibernate.validator.constraints.Length; import org.hibernate.validator.constraints.NotEmpty; @Entity @Table(name = "xx_admin") @SequenceGenerator(name = "sequenceGenerator", sequenceName = "seq_admin") public class Admin extends BaseEntity<Long> { private static final long serialVersionUID = -703349383658770247L; public static final String LOGIN_TOKEN_COOKIE_NAME = "adminLoginToken"; private String username; private String password; private String email; private String name; private String department; private Boolean isEnabled; private Boolean isLocked; private Integer loginFailureCount; private Date lockedDate; private Date loginDate; private String loginIp; private String lockKey; private Set<Role> roles = new HashSet<Role>(); @NotEmpty(groups = Save.class) @Pattern(regexp = "^[0-9a-zA-Z_\\u4e00-\\u9fa5]+$") @Length(min = 2, max = 20) @Column(nullable = false, updatable = false, unique = true) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @NotEmpty(groups = Save.class) @Length(min = 4, max = 20) @Column(nullable = false) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @NotEmpty @Email @Length(max = 200) @Column(nullable = false) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Length(max = 200) public String getName() { return name; } public void setName(String name) { this.name = name; } @Length(max = 200) public String getDepartment() { return department; } public void setDepartment(String department) { this.department = department; } @NotNull @Column(nullable = false) public Boolean getIsEnabled() { return isEnabled; } public void setIsEnabled(Boolean isEnabled) { this.isEnabled = isEnabled; } @Column(nullable = false) public Boolean getIsLocked() { return isLocked; } public void setIsLocked(Boolean isLocked) { this.isLocked = isLocked; } @Column(nullable = false) public Integer getLoginFailureCount() { return loginFailureCount; } public void setLoginFailureCount(Integer loginFailureCount) { this.loginFailureCount = loginFailureCount; } public Date getLockedDate() { return lockedDate; } public void setLockedDate(Date lockedDate) { this.lockedDate = lockedDate; } public Date getLoginDate() { return loginDate; } public void setLoginDate(Date loginDate) { this.loginDate = loginDate; } public String getLoginIp() { return loginIp; } public void setLoginIp(String loginIp) { this.loginIp = loginIp; } @Column(nullable = false, updatable = false) public String getLockKey() { return lockKey; } public void setLockKey(String lockKey) { this.lockKey = lockKey; } @NotEmpty @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "xx_admin_role") public Set<Role> getRoles() { return roles; } public void setRoles(Set<Role> roles) { this.roles = roles; } @PrePersist public void prePersist() { setUsername(StringUtils.lowerCase(getUsername())); setEmail(StringUtils.lowerCase(getEmail())); setLockKey(DigestUtils.md5Hex(UUID.randomUUID() + RandomStringUtils.randomAlphabetic(30))); } @PreUpdate public void preUpdate() { setEmail(StringUtils.lowerCase(getEmail())); } }