[英]What is the difference between @ComponentScan and @EnableAutoConfiguration in Spring Boot?
[英]What is the difference between @Entity and @Document in spring boot?
你能在你的數據庫表上同時使用這兩個注解嗎?
id,就像對差異的一些澄清。 謝謝你
@Entity 用來 map 一個 class 到一個關系數據庫,它代表一個數據庫表。
@Document 用於 map 到 class 到 noSQL 的數據庫(具體是 mongoDB),它代表了 MongoDB 個文檔。
如果您通過為每個數據庫創建不同的實體和存儲庫來使用這兩個數據庫,則可以同時使用 JPA 或 MongoRepository。
我建議您查看 spring 文檔 ( https://docs.spring.io/spring-data/jpa/docs/current/reference/html/ )
@Document
是 Spring Data Mongo 注釋,而@Entity
是 Java Persistence API (JPA) 的一部分。
您可以檢查兩個文檔:
在“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 { … }
文檔說:
PersonRepository
引用Person
,它用 JPA@Entity
注釋注釋,所以這個存儲庫顯然屬於 Spring 數據 JPA。UserRepository
引用User
,它用 Spring Data MongoDB 的@Document
注釋注釋。
所以你可以在這里看到區別。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.