简体   繁体   English

spring引导中的@Entity和@Document有什么区别?

[英]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 references Person , which is annotated with the JPA @Entity annotation, so this repository clearly belongs to Spring Data JPA. UserRepository references User , 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.

相关问题 Spring Boot中@ComponentScan和@EnableAutoConfiguration有什么区别? - What is the difference between @ComponentScan and @EnableAutoConfiguration in Spring Boot? Spring Entity Manager 和 Spring Data Repository 之间有什么区别? - What is the difference between an Spring Entity Manager and Spring Data Repository? Spring 和 Spring 引导之间的区别 - Difference between Spring and Spring Boot Spring Boot中的classpath和classpath *有什么区别? - what's the difference between classpath and classpath* in spring boot? Spring 启动 jpa 中的 @CreationTimestamp 和 @CreatedDate 有什么区别? - What's the difference between @CreationTimestamp and @CreatedDate in Spring boot jpa? spring-boot:run和spring-boot:start之间的区别是什么 - What's the difference between spring-boot:run and spring-boot:start spring-boot-test 与 spring-boot-starter-test 有什么区别? - what's the difference between spring-boot-test vs spring-boot-starter-test? JSP 和 Spring 有什么区别? - What is the difference between JSP and Spring? Spring Boot和Spring IO之间有什么关系? - What is the relationship between Spring Boot and Spring IO? 在 spring boot 中将属性放在 application.yml 或 bootstrap.yml 上有什么区别? - What is the difference between putting a property on application.yml or bootstrap.yml in spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM