Andrunevchyn

Andrunevchyn


November 2016
M T W T F S S
 123456
78910111213
14151617181920
21222324252627
282930  

Categories


Hibernate: comparing dates

Andriy AndrunevchynAndriy Andrunevchyn

Lets assume we have bean MyBean with field createTime

class MyBean{

public long id; 

public Date createTime; 

}

if you execute following code

MyBean bean = new MyBean(); 

Date time; bean.createTime = time; 

//save and get with hibernate 

save(bean); 

MyBean storedBean = getBeanById(bean.id); 

log.info("Times are equal: {}", storedBean.createTime.equals(time));

Variable ‘time’ was created and saved so that we can expect log will print “Times are equal true” but actually we’ll see “Times are equal false” When hibernate fetch Date from DB it returns java.sql.Date

That’s it

If you need such condition you should prefer

log.info("Times are equal: {}", storedBean.createTime.getTime() == time.getTime()); 

//returns Times are equal true

 

andriy@andrunevchyn.com

Comments 0
There are currently no comments.