public class User implements Serializable {
private static final long serialVersionUID = 1L;
/**
* ID,主键
*/
private Integer id;
/**
* 用户名
*/
private String name;
/**
* 昵称
*/
private String nickName;
/**
* 邮箱地址
*/
private String email;
/**
* 注册日期时间
*/
private Date registerDate;
/**
* 最近登录时间
*/
private Date recentLoginTime;
/**
* 上一次登录时间
*/
private Date lastLoginDay;
@Id
@GeneratedValue
public Integer getId() {
return id;
}
@Column(length = 18, nullable = false)
public String getName() {
return name;
}
@Transient
public String getNickName() {
return nickName;
}
@Column(name = "mail", length = 40, nullable = false)
public String getEmail() {
return email;
}
@Temporal(TemporalType.TIMESTAMP)
@Column(nullable = false)
public Date getRegisterDate() {
return registerDate;
}
@Temporal(TemporalType.TIME)
public Date getRecentLoginTime() {
return recentLoginTime;
}
@Temporal(TemporalType.DATE)
public Date getLastLoginDay() {
return lastLoginDay;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setNickName(String nickName) {
this.nickName = nickName;
}
public void setEmail(String email) {
this.email = email;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public void setRecentLoginTime(Date recentLoginTime) {
this.recentLoginTime = recentLoginTime;
}
public void setLastLoginDay(Date lastLoginDay) {
this.lastLoginDay = lastLoginDay;
}
}
注 : 注解可以是在属性或 getter 方法上进行声明,但不建议混合使用这两种声明方式,相反,应该尽量避免。另外,由 static 修饰的属性不会被持久化到数据库。
hibernate.cfg.xml 清单
Junit Test
package junit.test;
import java.util.Date;
import net.yeah.fancydeepin.po.User;
import org.hibernate.Session;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.BeforeClass;
import org.junit.Test;
/**
* -----------------------------------------
* @描述 Junit Test
* @作者 fancy
* @邮箱 fancydeepin@yeah.net
* @日期 2012-10-12
* -----------------------------------------
*/
public class TestApp {
private static Session session = null;
@BeforeClass
public static void beforeClass() throws Exception {
session = new AnnotationConfiguration().configure().buildSessionFactory().getCurrentSession();
}
@Test
public void createTable(){
new SchemaExport(new AnnotationConfiguration().configure()).create(true, true);
}
@Test
public void insert(){
User user = new User();
Date date = new Date();
user.setName("fancy");
user.setEmail("fancydeepin@yeah.net");
user.setRegisterDate(date);
user.setRecentLoginTime(date);
user.setLastLoginDay(date);
session.beginTransaction();
session.sav