HibernateUtil还包含了一些辅助方法,如: public static Object query(Class clazz, Serializable id); public static void createEntity(Object entity); public static Object queryForObject(String hql, Object[] params); public static List queryForList(String hql, Object[] params); 在此不再多述。 实体类User使用JPA注解,代表一个用户: @Entity @Table(name="T_USER") public class User { public static final String REGEX_USERNAME = "[a-z0-9][a-z0-9//-]{1,18}[a-z0-9]"; public static final String REGEX_PASSWORD = "[a-f0-9]{32}"; public static final String REGEX_EMAIL = "([0-9a-zA-Z]([-.//w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-//w]*[0-9a-zA-Z]//.)+[a-zA-Z]{2,9})"; private String username; // 用户名 private String password; // MD5口令 private boolean admin; // 是否是管理员 private String email; // 电子邮件 private int emailValidation; // 电子邮件验证码 private long createdDate; // 创建时间 private long lockDate; // 锁定时间 public User() {} public User(String username, String password, boolean admin, long lastSignOnDate) { this.username = username; this.password = password; this.admin = admin; } @Id @Column(updatable=false, length=20) @Pattern(regex=REGEX_USERNAME) public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } @Column(nullable=false, length=32) @Pattern(regex=REGEX_PASSWORD) public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Column(nullable=false, length=50) @Pattern(regex=REGEX_EMAIL) public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @Column(nullable=false) public boolean getAdmin() { return admin; } public void setAdmin(boolean admin) { this.admin = admin; } @Column(nullable=false, updatable=false) public long getCreatedDate() { return createdDate; } public void setCreatedDate(long createdDate) { this.createdDate = createdDate; } @Column(nullable=false) public int getEmailValidation() { return emailValidation; } public void setEmailValidation(int emailValidation) { this.emailValidation = emailValidation; } @Column(nullable=false) public long getLockDate() { return lockDate; } public void setLockDate(long lockDate) { this.lockDate = lockDate; } @Transient public boolean getEmailValidated() { return emailValidation==0; } @Transient public boolean getLocked() { return !admin && lockDate>0 && lockDate>System.currentTimeMillis(); } } 实体类PasswordTicket代表一个重置口令的请求: @Entity @Table(name="T_PWDT") public class PasswordTicket { private String id; private User user; private String ticket; private long createdDate; @Id @Column(nullable=false, updatable=false, length=32) @GeneratedValue(generator="system-uuid") @GenericGenerator(name="system-uuid", strategy="uuid") public String getId() { return id; } protected void setId(String id) { this.id = id; } @ManyToOne @JoinColumn(nullable=false, updatable=false) public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Column(nullable=false, updatable=false, length=32) public String getTicket() { return ticket; } public void setTicket(String ticket) { this.ticket = ticket; } @Column(nullable=false, updatable=false) public long getCreatedDate() { return createdDate; } public void setCreatedDate(long createdDate) { this.createdDate = createdDate; } } UserDao接口定义了对用户的相关操作: public interface UserDao { User queryForSignOn(String username); User queryUser(String username); void createUser(User user); void updateUser(User user); boolean updateEmailValidation(String username, int ticket); String createPasswordTicket(User user); boolean updatePassword(String username, String oldPassword, String newPassword); boolean queryResetPassword(User user, String ticket); boolean updateResetPassword(User user, String ticket, String password); void updateLock(User user, long lockTime); void updateUnlock(User user); } UserDaoImpl是其实现类: public class UserDaoImpl implements UserDao { public User queryForSignOn(String username) { User user = queryUser(username); if(user.getLocked()) throw new LockException(user.getLockDate()); return user; } public User queryUser(String username) { return (User) HibernateUtil.query(User.class, username); } public void createUser(User user) { user.setEmailValidation((int)(Math.random() * 1000000) + 0xf); HibernateUtil.createEntity(user); } // 其余方法略 ... } 由于将Hibernate事务绑定在Thread上,因此,实际的客户端调用DAO组件时,还必须加入事务代码: Transaction tx = HibernateUtil.getCurrentSession().beginTransaction(); try { dao.xxx(); tx.commit(); } catch(Exception e) { tx.rollback(); throw e; }  
说明:本教程来源互联网或网友上传或出版商,仅为学习研究或媒体推广,wanshiok.com不保证资料的完整性。
2/2 首页 上一页 1 2
|