Why do we need @Entity Annotation when we define the class in the configuration file

I am new to Hibernate. I went through several lessons, and I found that if we add the model class to the hibernate configuration file, we need to add the @Entity annotation in the model class.

Why is this so?

+5
source share
3 answers

@Entity - JPA annotation. From the JPA specification, section 2.1:

An entity class must be annotated with an Entity annotation or designated as an object in the XML descriptor.

So, if you use annotations for comparisons, @Entity is specified by the specification, and Hibernate must adhere to it.

Can JPA designers make this optional? Probably yes, but it really doesn’t take too much effort to always add it to entity classes to explicitly indicate that the class is an entity and thus makes it easier for future readers of the code to immediately understand the purpose of this class.

+2
source

I am quoting documentation for this

2.2.1. Mark POJO as a persistent entity

Each persistent POJO class is an entity and is declared using @Entity Annotation (at the class level):

@Entity public class Flight implements Serializable {Long id;

 @Id public Long getId() { return id; } public void setId(Long id) { this.id = id; } } 

@Entity declares the class as an entity (i.e., a constant POJO class), @Id declares the identifier property of this object. Other Display ads implicitly. The Flight class maps to the Flight Table using the column identifier as the primary key column. Note

The exception configuration concept is central to the JPA specification.

Depending on whether you comment on fields or methods, the type of access used by Hibernate will be a field or property. The EJB3 specification requires that you declare annotations for the type of element that will be accessed, that is, the getter method if you use property access, the field if you use field access. Mixing annotations in both fields and methods should be avoided. Hibernate guesses the type of access from @Id or @EmbeddedId.

There is something called the JPA aka Java Persistence API. Hibernate follows the guidelines for its implementation. So, to make sure it is correctly identified by the JVM (also displayed as an object)

JPA will include any class annotated with @Entity in the save control setting. You do not need persistence.xml if you use annotations. This applies to other JPA implementations as well as Open-JPA.

Usually thumb to Java. J2EE provides recommendations in the form of an interface and annotations in other ways. When someone is developing a J2EE API implementation, they follow these directives. This is a standardization method.

+1
source

In fact, you do not need annotation at all. Hibernate allows .xml configurations to be the only configuration source. But if you decide to use annotations, obviously you'll need @Entity in your model class.

Basically, you just need to tell Hibernate what your model classes are and how they map to database tables, and you can do this anyway - via .xml configurators or via JPA annotations. Moreover, you can even mix both approaches in one application, and there is nothing to prohibit.

+1
source

Source: https://habr.com/ru/post/1242798/


All Articles