43

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:

@Entity
@Table(name= “T_CUSTOMER”)
public class Customer {

    @Id
    @Column(name=“cust_id”)
    private Long id;

    @Column(name=“first_name”)
    private String firstName;

    @Transient
    private User currentUser;

    ...........................
    ...........................
    ...........................
}

Ok, I know that the @Entity annotation is on the class level and it means that the fields of the object that are instances of this class are to be mapped to the field of the T_CUSTOMER database table.

But why in JPA it is mandatory to use @Entity annotation and I cannot only use the @Table annotation to map a model object to a specific database table? It have some other meaning\behavior that actually I am missing?

What am I missing? What is the exact meaning of the @Entity annotation?

Naseer Mohammad
  • 389
  • 4
  • 14
AndreaNobili
  • 40,955
  • 107
  • 324
  • 596

3 Answers3

68

@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.

cegas
  • 2,823
  • 3
  • 16
  • 16
ioko
  • 991
  • 7
  • 4
  • 1
    If "@Entity annotation defines that a class can be mapped to a table", why didn't they just use @Table !? Would be much clearer! – Ian Vaughan Jan 08 '19 at 12:46
  • 1
    @IanVaughan Could be that this also shall support entities that are stored in a data store, which doesn't know tables, e.g. some nosql key-value stores? – Frank Hopkins Jan 28 '19 at 15:28
2

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

Entities represent persistent data stored in a relational database automatically using container-managed persistence.They are persistent because their data is stored persistently in some form of data storage system, such as a database: they do survive a server failure, failover, or a network failure. When an entity is reinstantiated, the state of the previous instance is automatically restored.

An entity models a business entity or multiple actions within a single business process. Entities are often used to facilitate business services that involve data and computations on that data. For example, you might implement an entity to retrieve and perform computation on items within a purchase order. Your entity can manage multiple, dependent, persistent objects in performing its tasks.

Entities can represent fine-grained persistent objects, because they are not remotely accessible components.

An entity can aggregate objects together and effectively persist data and related objects using the transactional, security, and concurrency services of a JPA persistence provider.