首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring数据MongoDB -注释@CreatedDate在使用自定义Id字段时不起作用

Spring数据MongoDB -注释@CreatedDate在使用自定义Id字段时不起作用
EN

Stack Overflow用户
提问于 2017-11-01 12:20:29
回答 6查看 13.3K关注 0票数 7

我有一个简单的持久课程:

代码语言:javascript
运行
复制
public class Profile implements Persistable<String>{

    @Id
    private String username;

    @CreatedDate
    public Date createdDate;

    public Profile(String username) {
        this.username = username;
    }

    @Override
    public String getId() {
        return username;
    }

    @Override
    public boolean isNew() {
        return username == null;
    }
}

和一个简单的存储库:

代码语言:javascript
运行
复制
public interface ProfileRepository extends MongoRepository<Profile, String> {

}

我的Spring类也被注释为@EnableMongoAudting.,但是我仍然无法得到注释@CreatedDate。

ProfileRepository.save(新配置文件(“user1”))在没有字段createdDate的情况下写入实体。我做错什么了?

编辑:--这是我的应用程序类(没有@EnableMongoRepositories,但它可以工作,因为存储库在我猜的子包中)

代码语言:javascript
运行
复制
@SpringBootApplication
@EnableMongoAuditing
public class Application {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

编辑:也添加了注释EnableMongoRepositories没有改变任何东西。

EN

回答 6

Stack Overflow用户

发布于 2018-08-15 13:04:08

只需将@Version字段添加到@Document类中,然后离开@EnableMongoAuditing,即

代码语言:javascript
运行
复制
@Document
public class Profile implements Persistable<String>{

     @Version      
     private Long version;
    
     @Id
     private String username;

     @CreatedDate
     public Date createdDate;

     public Profile(String username) {
         this.username = username;
     }

     @Override
     public String getId() {
         return username;
     }

     @Override
     public boolean isNew() {
         return username == null;
     }
 }

下面是一个相关的问题:https://jira.spring.io/browse/DATAMONGO-946

票数 7
EN

Stack Overflow用户

发布于 2017-11-03 10:15:03

我自己也遇到了这个问题,这是因为你自己正在创建id。

代码语言:javascript
运行
复制
public Profile(String username) {
        this.username = username;
    }

通过这样做,mongo认为它不是一个新对象,并且不使用@CreatedDate注释。您还可以使用@Document注释,而不是实现Persistable类,如下所示:

代码语言:javascript
运行
复制
@Document
public class Profile{}
票数 5
EN

Stack Overflow用户

发布于 2021-01-13 22:17:31

正如Spring发布DATAMONGO-946中描述的那样,创建的日期功能使用isNew()方法来确定是否应该设置创建的日期,因为该实体是新的。在这种情况下,您的isNew方法总是返回false,因为username总是被设置的。

该问题的评论提出了解决这一问题的两种可能的解决办法。

Persistable

第一个选项是修复isNew策略,以便它正确地注册新对象。注释中建议的一种方法是更改实现以检查createdDate字段本身,因为它只应该设置在非新对象上。

代码语言:javascript
运行
复制
@Override
public boolean isNew() {
    return createdDate == null;
}

持久实体解决方案

第二个选择是从实现Persistable改为使用持久实体,并使用@Version注释在持久化的MongoDB实体中注入version属性。注意,这将改变数据的持久化方式,因为它向数据中添加了一个自动递增的version字段。

代码语言:javascript
运行
复制
import org.springframework.data.mongodb.core.mapping.Document;

@Document
public class Profile {
    @Id
    private String username;

    @CreatedDate
    public Date createdDate;

    @Version
    public Integer version;

    public Profile(String username) {
        this.username = username;
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47054719

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档