[英]What is the difference between @Entity and @Document in spring boot?
Can you use both annotations on your database tables?你能在你的数据库表上同时使用这两个注解吗?
id, just like some clarification on there differences. id,就像对差异的一些澄清。 thank you谢谢你
@Entity is used to map a class to a relational database, it represents a database table. @Entity 用来 map 一个 class 到一个关系数据库,它代表一个数据库表。
@Document is used to map a class to noSQL database (specifically mongoDB), it represents a MongoDB documents. @Document 用于 map 到 class 到 noSQL 的数据库(具体是 mongoDB),它代表了 MongoDB 个文档。
You can use both JPA or MongoRepository if you are using both databases by creating different entities and repositories for each database.如果您通过为每个数据库创建不同的实体和存储库来使用这两个数据库,则可以同时使用 JPA 或 MongoRepository。
I recommend you to have a look at spring documentation ( https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ )我建议您查看 spring 文档 ( https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ )
@Document
is a Spring Data Mongo annotation while @Entity
is part of Java Persistence API (JPA). @Document
是 Spring Data Mongo 注释,而@Entity
是 Java Persistence API (JPA) 的一部分。
You can check both documentations:您可以检查两个文档:
Where into "Example 10. Repository definitions using domain classes with annotations" there is this piece of code:在“Example 10. Repository definitions using domain classes with annotations”中有这段代码:
interface PersonRepository extends Repository<Person, Long> { … }
@Entity
class Person { … }
interface UserRepository extends Repository<User, Long> { … }
@Document
class User { … }
And documentation says:文档说:
PersonRepository
referencesPerson
, which is annotated with the JPA@Entity
annotation, so this repository clearly belongs to Spring Data JPA.UserRepository
referencesUser
, which is annotated with Spring Data MongoDB's@Document
annotation.PersonRepository
引用Person
,它用 JPA@Entity
注释注释,所以这个存储库显然属于 Spring 数据 JPA。UserRepository
引用User
,它用 Spring Data MongoDB 的@Document
注释注释。
So you can see here the difference.所以你可以在这里看到区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.