hibernate中java.util.Date类型映射
2009-12-21 00:00:00 来源:WEB开发网在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类型。
更多精彩
赞助商链接