hibernate中java.util.Date类型映射
时间:2011-03-25 javaeye 青蜂侠
在Hibernate中对于java.util.Date类型的映射为:
java类型 |
Hibernate类型 |
Mysql类型 |
java.util.Date、java.sql.Date |
date |
DATE |
java.util.Date、java.sql.Time |
time |
TIME |
java.util.Date、java.sql.Timestamp |
timestamp |
TIMESTAMP |
如果使用timestamp作为Date类型映射时,具体代码如下:
Java代码
public class User {
private String userPK;
private String userID;
private String password;
private String nickname;
private boolean state;
private boolean role;
private Date registerDate;
private int points;
public String getUserPK() {
return userPK;
}
private void setUserPK(String userPK) {
this.userPK = userPK;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public Date getRegisterDate() {
return registerDate;
}
public void setRegisterDate(Date registerDate) {
this.registerDate = registerDate;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
public void setState(boolean state) {
this.state = state;
}
public boolean getState(){
return this.state;
}
public void setRole(boolean role) {
this.role = role;
}
public boolean getRole(){
return this.role;
}
}
hibernate中java.util.Date类型映射(2)
时间:2011-03-25 javaeye 青蜂侠
映射代码:
Xml代码
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="org.brucefeng.sinter.domain">
<class name="User" table="sinter_user">
<id name="userPK" column="userpk" unsaved-value="null">
<generator class="uuid.hex"></generator>
</id>
<property name="userID" type="string" column="userid" unique="true"></property>
<property name="password" type="string" column="password"></property>
<property name="nickname" type="string" column="name"></property>
|