In general, entity is a group of states associated together in a single unit. On adding behavior, an entity behaves as an object and becomes a major constituent of object-oriented paradigm. So, an entity is an application-defined object in Java Persistence Library.
Entity Properties
These are the properties of an entity that an object must have: -
- Persistability - An object is called persistent if it is stored in the database and can be accessed anytime.
- Persistent Identity - In Java, each entity is unique and represents as an object identity. Similarly, when the object identity is stored in a database then it is represented as persistence identity. This object identity is equivalent to primary key in database.
- Transactionality - Entity can perform various operations such as create, delete, update. Each operation makes some changes in the database. It ensures that whatever changes made in the database either be succeed or failed atomically.
- Granularity - Entities should not be primitives, primitive wrappers or built-in objects with single dimensional state.
Entity Metadata
Each entity is associated with some metadata that represents the information of it. Instead of database, this metadata exists either inside or outside the class.
This metadata can be in following forms: -
Annotation - In Java, annotations are the form of tags that represents metadata. This metadata persist inside the class.
XML - In this form, metadata persist outside the class in XML file.