在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;
}
}
映射代碼:
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>
<property name="state" type="boolean" column="state"></property>
<property name="role" type="boolean" column="role"></property>
<property name="points" type="integer" column="points"></property>
<property name="registerDate" type="timestamp" column="registerdate"></property>
</class>
</hibernate-mapping>
Java代碼
User testUser = new User();
testUser.setRegisterDate(new Date);
session.save(testUser);
存儲結束之後,由數據庫中重新查詢出此對象:
Java代碼
User savedUser = session.load(User.class,id);
而此時
testUser.getRegisterDate.equals(savedUser.getRegisterDate)返回值為 false;
此時可發現test.getRegisterDate().class為java.util.Date, savedUser.getRegisterDate().class為java.sql.Timestamp。查看java的文檔可知
java doc 寫道
The Timestamp.equals(Object) method never returns true when passed an object that isn't an instance of java.sql.Timestamp, because the nanos component of a date is unknown. As a result, the Timestamp.equals(Object) method is not symmetric with respect to the java.util.Date.equals(Object) method.
也就是說Timestamp和Date類型並不能進行相等比較的。當然在程序中比較兩個時間點 的相等性是不切合實際的,但如果一些特殊應用需要比較的話,可以使用其他方法解決。 例如:
Java代碼
public void setRegisterDate(Date registerDate) {
this.registerDate = new Date(registerDate.getTime());
}
這樣即可順利轉變類型及值了。但是Hibernate中並沒有對java.util.Date類型進行很 好的映射,個人也覺得可以不采用Date類型記錄時間,可適當考慮Calendar類型。