Can you use both annotations on your database tables?
id, just like some clarification on there differences. thank you
CodePudding user response:
@Entity is used to map a class to a relational database, it represents a database table.
@Document is used to map a class to noSQL database (specifically mongoDB), it represents a MongoDB documents.
You can use both JPA or MongoRepository if you are using both databases by creating different entities and repositories for each database.
I recommend you to have a look at spring documentation (https://docs.spring.io/spring-data/jpa/docs/current/reference/html/)
CodePudding user response:
@Document
is a Spring Data Mongo annotation while @Entity
is part of 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:
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.
So you can see here the difference.