Spring Roo Date Field with Current Timestamp

五迷三道 提交于 2019-12-02 19:31:15

问题


Most databases allow to have field with current timestamp (act as creation timestamp), for example in MySQL:

CREATE TABLE t (ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP)

How to achieve this in Spring Roo? Can't find hint from Roo manual.

Thanks.


回答1:


This would be created with:

field date --fieldName ts --type java.util.Date --persistenceType JPA_TIMESTAMP

This will add:

 @Temporal(TemporalType.TIMESTAMP) 

to the field, which should cause the auto-generator to create a TIMESTAMP field. If you want more control, you can always annotate the generated entity field further with

 @Column(name="ts", columnDefinition="TIMESTAMP DEFAULT CURRENT_TIMESTAMP")



回答2:


This can be done as suggested by Simon, but it might be worth checking out a fuller solution.

Check out this Spring Roo Timestamp add-on. It adds 'created' and 'updated' timestamps to all entities marked with its annotation. https://github.com/rcaloras/spring-roo-addon-timestamp

(I created it, happy to answer questions or add additional features)



来源:https://stackoverflow.com/questions/5549905/spring-roo-date-field-with-current-timestamp

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