为什么对 MongoDB 域对象使用空白的@Document?
Why use a blank @Document for MongoDB domain objects?
我见过很多代码以下列方式为 MongoDB 定义域对象:
带有空白的@Document 注释:
@Document
class Customer {
int id;
String name;
}
没有 @Document
注释:
class Customer {
int id;
String name;
}
集合名称:
@Document(collection = "cust")
class Customer {
int id;
String name;
}
我能理解最后两个,但是你为什么只想保留 @Document
注释?
Spring 文档指出:
7.3。基于元数据的映射
To take full advantage of the object mapping functionality inside the
Spring Data/MongoDB support, you should annotate your mapped objects
with the @org.springframework.data.mongodb.core.mapping.Document
annotation. Although it is not necessary for the mapping framework to
have this annotation (your POJOs will be mapped correctly, even
without any annotations), it allows the classpath scanner to find and
pre-process your domain objects to extract the necessary metadata. If
you don't use this annotation, your application will take a slight
performance hit the first time you store a domain object because the
mapping framework needs to build up its internal metadata model so it
knows about the properties of your domain object and how to persist
them.
https://docs.spring.io/spring-data/data-document/docs/current/reference/html/
我见过很多代码以下列方式为 MongoDB 定义域对象:
带有空白的@Document 注释:
@Document
class Customer {
int id;
String name;
}
没有 @Document
注释:
class Customer {
int id;
String name;
}
集合名称:
@Document(collection = "cust")
class Customer {
int id;
String name;
}
我能理解最后两个,但是你为什么只想保留 @Document
注释?
Spring 文档指出:
7.3。基于元数据的映射
To take full advantage of the object mapping functionality inside the Spring Data/MongoDB support, you should annotate your mapped objects with the @org.springframework.data.mongodb.core.mapping.Document annotation. Although it is not necessary for the mapping framework to have this annotation (your POJOs will be mapped correctly, even without any annotations), it allows the classpath scanner to find and pre-process your domain objects to extract the necessary metadata. If you don't use this annotation, your application will take a slight performance hit the first time you store a domain object because the mapping framework needs to build up its internal metadata model so it knows about the properties of your domain object and how to persist them.
https://docs.spring.io/spring-data/data-document/docs/current/reference/html/