Recently wrote a deletion and modification of the east, where the modification of the function related to the comparison of time, this place found a problem, the problem in our development environment does not exist, but after the release of an int after the release of the administrator came out. It turns out that this actually involves the JDK version, but it's also a problem.
First, the Time field type defined in the database is the date type, and the ORM mapping in Hibernate. hbm.xml files is the code in Java.util.date,javabean and Java.util.date,hbm.xml:
<property name= "Tbedindate" type= "java.util.Date" > <column name= "T_bedin_date" length= "7" > <comment> start </comment> </column> </property> <property name= "Tenddate" Type= "Java.util.Date" > <column name= "T_end_date" length= "7" > <comment> Stop period </comment> </column> </property>
When the function is modified, the data from the page into the background is compared with the query data in the database, because these two time fields are required fields, I used the java.util.Date CompareTo function directly to compare, this is the problem, after the jdk1.5 version of the JDK compiled, there will be problems.
// if (Objold.gettenddate (). CompareTo (Obj.gettenddate ())!=0) {// objold.settenddate ( Obj.gettenddate ()); // }
I then analyzed the problem: the type of the time field that was queried first from the database, Hibernate has been converted by default to: Java.sql.Timestamp type, and the data obtained from the data after the page display, the Returned Time field is normal JavaBean defined in the Java.util.Date type, when using the CompareTo to compare the time will be thrown Out of the exception, since this is already two different types in the comparison.
There are several ways to solve this problem, you can turn timestamp into date, you can turn date into timestamp, and then compare, I use the method is to directly convert two time fields into a fixed format string, and then compare.
1, all turn into string and then compare:
New SimpleDateFormat ("Yyyy-mm-dd"). Format (Objold.gettenddate ()); New SimpleDateFormat ("Yyyy-mm-dd"). Format (Obj.gettenddate ()); if (! tenddate.equals (tenddatenew)) { objold.settenddate (obj.gettenddate ()); }
2. Timestamp turns into date
Timestamp t = new Timestamp (System.currenttimemillis ()); Date D = new date (T.gettime ());
3. Date turns into timestamp
New SimpleDateFormat ("Yyyy-mm-dd:hh:mm:ss"=new SimpleDateFormat ("Yyyy-mm-dd:hh:mm:ss"). Format ( Objold.gettenddate ());
Timestamp= timestamp.valueof (TSSTR);
or with new Timestamp (New Date ()). GetTime ()), this has not been tried.
about why the type of the time field queried from the database will be hibernate by default to the timestamp type, looked up a lot of information, there is no reason to give the person convincing,
In the search for information, found a few look very good article, you can learn the following:
Java.util.Date, Java.sql.Date, Java.sql.Time, java.sql.Timestamp differences and summaries: http://blog.csdn.net/xiancaieeee/article/ details/8099184
Two key issues with JDK 1.4 upgrade to JDK5 JDK6 BigDecimal, java.sql.date:http://shuwen.iteye.com/blog/1179826
Problems of java.util.Date change timestamp in date type and JavaBean in Hibernate mappings