Hibernate to persist Dates as long

ぃ、小莉子 提交于 2020-01-02 09:21:06

问题


Is there a way to tell Hibernate that java.util.Date should be persisted as long? I need this to get around the missing millisecond resolution in MySQL.

Could you think of any drawbacks of this approach?


回答1:


you can do it with a virtual attribute.

So you map a long, and add a get/set for a Date object. The setter sets the long value and the getter creates a new Date with the long.

And I can't see any drawbacks, only that probably you want to use a Date in your app (that's why I think the virtual attribute solution is goodish)




回答2:


You can make the field itself long. Or use a custom UserType. After implementing UserType, configure it on the field by @Type(type = "com.yourcompany.YourUserType")



来源:https://stackoverflow.com/questions/5857926/hibernate-to-persist-dates-as-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!