I am new to Hibernate. I was going through some tutorials and I found that if we are adding the model class in hibernate configuration file, still we need to add @Entity
annotation in the model class.
Why is it so?
@Entity
is a JPA annotation. From the JPA specification, section 2.1:
The entity class must be annotated with the Entity annotation or denoted in the XML descriptor as an entity.
So, if you use annotations for mappings, @Entity
is mandated by the specification and Hibernate has to adhere to it.
Could JPA designers make it optional? Probably yes, but it really does not cost too much of effort to always add it to entity classes to explicitly designate that a class is an entity and thus make it easier for the future readers of the code to immediately see the purpose of the class.
I would quote the documentation for this
2.2.1. Marking a POJO as persistent entity
Every persistent POJO class is an entity and is declared using the @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 persistent POJO class), @Id declares the identifier property of this entity. The other mapping declarations are implicit. The class Flight is mapped to the Flight table, using the column id as its primary key column. Note
The concept of configuration by exception is central to the JPA specification.
Depending on whether you annotate fields or methods, the access type used by Hibernate will be field or property. The EJB3 spec requires that you declare annotations on the element type that will be accessed, i.e. 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 will guess the access type from the position of @Id or @EmbeddedId.
There is something called JPA aka Java Persistence API. Hibernate follows the guidelines in its implementation. Thus to make sure it gets identified correctly by JVM (as well get mapped as entity)
JPA will include any class annotated with @Entity in the persistence management setup. You don't need persistence.xml if you use annotations. This goes in other JPA implementations as well like Open-JPA
As a thumb rule to Java. J2EE provides guidelines in the form of interface and annotations and various other ways. When someone develops an API implementation of J2EE they follow these directives. This is the way of standardization.
Actually you do not strictly need annotation at all. Hibernate allows its .xml
configs to be a single source of configuration. But if you choose to use annotations - obviously you will need @Entity
at your model class.
Basically, you just need to tell Hibernate what are your model classes and how do they map to database tables, and you can do it either way - via .xml
configs or via JPA annotations. Moreover, you can even mix both approaches in a single application, and there is nothing to prohibit this.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With