I generate a single date and save it to the database through sleep mode, and when I get the value and I compare it with the value before inserting it. The result is not equal!
I created the date as follows
Date rightnow = Calendar.getInstance().getTime(); Task t1 = new Task("My task", rightnow); taskDao.saveOrUpdate(t1); Task taskR1 = taskDao.get(t1.getIdTask()); assertEquals("They should have to be equal dates",taskR1.getDate(),t1.getDate());
I get this error
<2014-04-11 23:13:13.0> differs from <Fri Apr 11 23:13:13 CEST 2014>
java.lang.AssertionError: They should have to be equal dates expected:<2014-04-11 23:13:13.0> but was:<Fri Apr 11 23:13:13 CEST 2014>
Additional information related to the problem
Class task
@Entity @Table(name = "t_task") public class Task { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "idTask") private long idTask; ... @Column(name = "date") private Date date; ...
Mysql t_task table
CREATE TABLE IF NOT EXISTS `mytask`.`t_task` ( `idTask` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT, `date` DATETIME NOT NULL ...
I created new hashCode () and equals () functions in Task with only a date field and even both.
@Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((date == null) ? 0 : date.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (!(obj instanceof Task)) return false; Task other = (Task) obj; if (date == null) { if (other.date != null) return false; } else if (!date.equals(other.date)) return false; return true; }
Any idea?
source share