简体   繁体   中英

@Temporal(TemporalType.TIMESTAMP)

Is there anything similar for mongoDB? @Temporal(TemporalType.TIMESTAMP)

@NotNull
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(style = "M-")
private Date lastUpdateDate;

I am trying to create a field that will automatically update the "lastUpdateDate" whenever the the document is updated.

OR

In spring roo

field date --fieldName lastUpdateDate --type java.util.Date --notNull --persistenceType JPA_TIMESTAMP

If what you want is ' create a field that will automatically update the "lastUpdateDate" ', then you can use the annotations: @LastModifiedDate and @EnableMongoAuditing.

@SpringBootApplication
@EnableMongoAuditing
public class Application {...}

public class ModelClass implements Persistable<String> {
    @LastModifiedDate
    private Date lastModifiedDate;
    ...
}

based on: http://www.aichengxu.com/java/2261025.htm

Looking around I'm surprised to see that there's not a good generic solution for this kind of thing. Like @CodeChimp said, it shouldn't be hard to provide this kind of thing out of the box.

One possible solution could be creating an annotation to tag your models with and then using AOP, but that'd take a little work. This article shows a somewhat naive approach to doing something like that. I copied the code to a gist to give it a quick look, and it seems like it could easily be modified for what you're looking to do.

I'll try to circle back this weekend and put together an actual example, but you may want to take a look to see if you're comfortable with that kind of solution and interested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM