Spring开机MongoDB带版本注解的字段不自增
Spring Boot MongoDB the field with version annotation does not increment
这个问题之前问过好几次了,但是这些问题的答案都不适合我。
这是我的图书文件
@Document
@NoArgsConstructor
@ToString
@Getter
@Setter
@EqualsAndHashCode
public class Book
{
@Id
private String id;
@Indexed
private String name;
private Double price;
private Integer stock;
@Version
private Integer version;
public Book(String name, Integer stock, Double price)
{
this.name = name;
this.stock = stock;
this.price = price;
}
}
这是我的 bookRepository 实现
public interface BookRepository extends MongoRepository<Book, String>
{
}
这是我的 customBookRepository 实现
@Repository
public class CustomBookRepository
{
private final BookRepository bookRepository;
@Autowired
private MongoTemplate mongoTemplate;
public CustomBookRepository(BookRepository bookRepository)
{
this.bookRepository = bookRepository;
}
public Book save(Book book)
{
return bookRepository.save(book);
}
public Optional<Book> findById(String id)
{
return bookRepository.findById(id);
}
public boolean updateQuantity(Integer stock, String id)
{
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id)); // (1)
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("stock", stock); // (2)
BasicDBObject updateObject = new BasicDBObject();
updateObject.put("$set", newDocument); // (3)
final UpdateResult book = mongoTemplate.getCollection("book").updateOne(query, updateObject);
return book.wasAcknowledged();
}
}
版本没有改变,在我每次更新实体时它都保持为 0。 (例如 updateQuantity())
我试过添加注解@EnableMongoAuditing
@SpringBootApplication
@EnableMongoAuditing
public class ReadingIsGoodApplication
{
public static void main(String[] args)
{
SpringApplication.run(ReadingIsGoodApplication.class, args);
}
}
它没有改变任何东西。
不要使用“自定义存储库”
使用 jpa 方法 save/update。然后将更新版本字段。
这个问题之前问过好几次了,但是这些问题的答案都不适合我。
这是我的图书文件
@Document
@NoArgsConstructor
@ToString
@Getter
@Setter
@EqualsAndHashCode
public class Book
{
@Id
private String id;
@Indexed
private String name;
private Double price;
private Integer stock;
@Version
private Integer version;
public Book(String name, Integer stock, Double price)
{
this.name = name;
this.stock = stock;
this.price = price;
}
}
这是我的 bookRepository 实现
public interface BookRepository extends MongoRepository<Book, String>
{
}
这是我的 customBookRepository 实现
@Repository
public class CustomBookRepository
{
private final BookRepository bookRepository;
@Autowired
private MongoTemplate mongoTemplate;
public CustomBookRepository(BookRepository bookRepository)
{
this.bookRepository = bookRepository;
}
public Book save(Book book)
{
return bookRepository.save(book);
}
public Optional<Book> findById(String id)
{
return bookRepository.findById(id);
}
public boolean updateQuantity(Integer stock, String id)
{
BasicDBObject query = new BasicDBObject();
query.put("_id", new ObjectId(id)); // (1)
BasicDBObject newDocument = new BasicDBObject();
newDocument.put("stock", stock); // (2)
BasicDBObject updateObject = new BasicDBObject();
updateObject.put("$set", newDocument); // (3)
final UpdateResult book = mongoTemplate.getCollection("book").updateOne(query, updateObject);
return book.wasAcknowledged();
}
}
版本没有改变,在我每次更新实体时它都保持为 0。 (例如 updateQuantity())
我试过添加注解@EnableMongoAuditing
@SpringBootApplication
@EnableMongoAuditing
public class ReadingIsGoodApplication
{
public static void main(String[] args)
{
SpringApplication.run(ReadingIsGoodApplication.class, args);
}
}
它没有改变任何东西。
不要使用“自定义存储库”
使用 jpa 方法 save/update。然后将更新版本字段。