An annotation is declarative metadata attached to a program element. Persistence frameworks use annotations to describe mappings, validation, identifiers, indexes, constructors, and fields that should not be stored. section 3.3 section 17.2
Mapping a Java class
A JPA entity can be declared directly beside the class it describes: import javax.persistence.Entity; @Entity public class Item { } The annotation is compiled into the class file, remains available through reflection, and can be validated and refactored by Java-aware tools. A provider can also add vendor-specific annotations for features such as caching. section 3.3.1
Common annotation roles include:
- Declare persistence: @Entity identifies a JPA entity, while @Document identifies a MongoDB document. section 3.3.1 section 17.2
- Control stored state: @Id marks an identifier, and @Transient excludes a field from persistence. section 17.2
- Configure database structures: @Indexed and @CompoundIndex define MongoDB indexes. section 17.2
- Select construction and generated code: @PersistenceConstructor chooses a retrieval constructor, while Lombok annotations generate constructors, getters, and setters. section 17.2
import javax.persistence.Entity;
@Entity
public class Item {
}
Use the minimal @Entity listing to inspect how persistence metadata sits directly beside the Java class declaration. section 3.3.1
