亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring Boot mongo審計@version問題

Spring Boot mongo審計@version問題

手掌心 2022-01-12 10:14:30
我剛開始一個新項目,想使用 Sprint Boot 2.1,一開始就遇到了問題。我想做的是使用 Spring Boot Mongo 來管理數據庫。我想要一個帶有@Version注釋的樂觀鎖。但是,我發現它似乎@Version會影響save()MongoRepository 中的行為,這意味著 dup key 錯誤。以下是示例代碼。POJO    @Data    @AllArgsConstructor    @NoArgsConstructor    @Document    public class Person {        @Id public ObjectId id;        @CreatedDate public LocalDateTime createOn;        @LastModifiedDate public LocalDateTime modifiedOn;        @Version public long version;        private String name;        private String email;        public Person(String name, String email) {            this.name = name;            this.email = email;        }       @Override        public String toString() {            return String.format("Person [id=%s, name=%s, email=%s, createdOn=%s, modifiedOn=%s, version=%s]", id, name, email, createOn, modifiedOn, version);        }    }MongoConfig    @Configuration    @EnableMongoRepositories("com.project.server.repo")    @EnableMongoAuditing    public class MongoConfig {    }存儲庫    public interface PersonRepo extends MongoRepository<Person, ObjectId> {        Person save(Person person);        Person findByName(String name);        Person findByEmail(String email);        long count();        @Override        void delete(Person person);    }如Official Doc 中所示,我的version字段位于 中long,但 dup 鍵錯誤發生在 second save,這意味著它insert再次嘗試,即使對象中的 id 也是如此。我也嘗試了Longinversion字段,它沒有發生 dup 鍵并按預期保存為更新,但createdOn成為null第一個save(這意味著insert)控制器Person joe = new Person("Joe", "[email protected]");System.out.println(joe.toString());this.personRepo.save(joe);Person who = this.personRepo.findByName("Joe");System.out.println(who.toString());who.setEmail("[email protected]");this.personRepo.save(who);Person who1 = this.personRepo.findByName("Joe");Person who2 = this.personRepo.findByEmail("[email protected]");System.out.println(who1.toString());System.out.println(who2.toString());據我所知,spring 使用 id 存在作為保存行為控制,這意味著如果 id 存在,那么保存就像 mongo 的插入一樣。但是,在這里,版本似乎也會影響保存行為或影響spring識別id存在的方式。問題:如何將 MongoAudit 與 MongoRepository 一起使用?我犯了任何錯誤/錯誤嗎?
查看完整描述

2 回答

?
UYOU

TA貢獻1878條經驗 獲得超4個贊

對于使用@Version,您必須首先從數據庫中檢索數據模型,并且在更新數據后,您必須將相同的數據保存到數據庫中。例如:


  personRepo.findByName(name).ifPresent(person-> {

            person.setEmail("[email protected]");

            personRepo.save(person);

            log.info("Updated Data: {}", person);

        });

@CreatedDatenull如果您沒有將其添加@Version到您的模型類中,將永遠如此。它適用于@Version


如果您沒有添加@Version到模型類并且您嘗試使用具有模型類來更新相同的模型類@Version,那么這里還要添加一點,它會再次給您重復 id 錯誤。


查看完整回答
反對 回復 2022-01-12
?
牛魔王的故事

TA貢獻1830條經驗 獲得超3個贊

我仍然無法弄清楚問題所在。但是,即使我的設置與上面的帖子完全相同,由于我將 Spring Boot 從 2.1.0 升級到 2.1.1,現在一切正常(無論我使用什么類型的版本,Long/long)


以下是我現在正在使用的庫版本。


spring-boot-starter-data-mongodb:2.1.1.RELEASE:

  -> spring-data-mongo:2.1.3.RELEASE

  -> mongodb-driver:3.8.2



查看完整回答
反對 回復 2022-01-12
  • 2 回答
  • 0 關注
  • 313 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號