问题
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