What is the exact meaning of the JPA @Entity annotation?

前端 未结 2 581
渐次进展
渐次进展 2021-01-31 16:39

I am studying JPA in Spring application and I have some doubts related to the @Entity annotation.

So I have a model class like this:

@En         


        
相关标签:
2条回答
  • 2021-01-31 17:16

    @Entity annotation defines that a class can be mapped to a table. And that is it, it is just a marker, like for example Serializable interface.

    And why @Entity annotation is mandatory? ... well, it is the way how JPA is designed. When you create a new entity you have to do at least two things

    1. annotated it with @Entity

    2. create an id field and annotate it with @Id

    Anything else is optional, for example table name is derived from entity class name (and therefore @Table annotation can be optional), table's columns are derived from entities variables (and therefore @Column annotation can be optional), and so on ...

    JPA is trying to provide a fast and easy start to developers who want to learn/use this API, and giving developers option to configure as few things as possible to make something functional is one of the ways how this API wants to achieve this "easy to use/learn" goal. Hence the @Entity annotation (together with @Id annotation) is the minimum you have to do in order to create an entity.

    0 讨论(0)
  • 2021-01-31 17:17

    Entities in JPA are nothing but POJOs representing data that can be persisted to the database. An entity represents a table stored in a database. Every instance of an entity represents a row in the table.

    More about the entities: https://www.baeldung.com/jpa-entities

    0 讨论(0)
提交回复
热议问题