开发学院软件开发Java hibernate中java.util.Date类型映射 阅读

hibernate中java.util.Date类型映射

 2009-12-21 00:00:00 来源:WEB开发网   
核心提示:在Hibernate中对于java.util.Date类型的映射为:java类型Hibernate类型Mysql类型java.util.Date、java.sql.DatedateDATEjava.util.Date、java.sql.TimetimeTIMEjava.util.Date、java.sql.Timest

在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类型。

Tags:hibernate java util

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
更多精彩
    赞助商链接

    热点阅读
      焦点图片
        最新推荐
          精彩阅读