Top 100 Annotations In Spring Data JPA

Spring Data JPA offers a variety of annotations to enhance the functionality of your data access layer. While there are many annotations available, I will provide a list of commonly used Spring Data JPA annotations:

1. @Entity: 

Marks a class as a persistent entity that can be mapped to a database table.

2. @Table: 

Specifies the table name and other attributes for an entity.

3. @Id: 

Defines a field as the primary key of an entity.

4. @GeneratedValue: 

Indicates that the value of the annotated field is generated automatically (e.g., using an identity column or a sequence).

5. @Column: 

Specifies the mapping between an entity field and a database column, allowing customization of column name, length, nullable, etc.

6. @Transient: 

Excludes a field from being persisted in the database.

7. @OneToMany: 

Establishes a one-to-many relationship between entities.

8. @ManyToOne: 

Establishes a many-to-one relationship between entities.

9. @OneToOne: 

Establishes a one-to-one relationship between entities.

10. @ManyToMany: 

Establishes a many-to-many relationship between entities.

11. @JoinColumn: 

Specifies the mapping of a foreign key column in a relationship.

12. @JoinTable: 

Configures the join table for a many-to-many relationship.

13. @NamedQuery: 

Defines a named query using JPQL (Java Persistence Query Language).

14. @NamedNativeQuery: 

Defines a named native SQL query.

15. @Query: 

Specifies a custom query using JPQL or native SQL within a repository method.

16. @Param: 

Binds a method parameter to a named parameter in a query.

17. @Modifying: 

Indicates that a query is modifying the database state (e.g., insert, update, delete).

18. @Transactional: 

Defines the transactional behavior of a method or class.

19. @Lock: 

Specifies the locking strategy for a query or a method.

20. @EntityGraph: 

Specifies the entity graph to be used for a query.

21. @Version: 

Enables optimistic locking by using a version attribute.

22. @EnableJpaRepositories: 

Enables Spring Data JPA repositories in a configuration class.

23. @Repository: 

Marks an interface or class as a Spring Data repository.

24. @QueryHints: 

Specifies query hints to optimize the query execution.

25. @PersistenceContext: 

Injects the EntityManager into a bean.

26. @PersistenceUnit: 

Injects the EntityManagerFactory into a bean.

27. @EntityListeners: 

Registers entity listener classes for lifecycle events.

28. @PrePersist: 

Specifies a method to be executed before an entity is persisted.

29. @PostPersist: 

Specifies a method to be executed after an entity is persisted.

30. @PreUpdate: 

Specifies a method to be executed before an entity is updated.

31. @PostUpdate: 

Specifies a method to be executed after an entity is updated.

32. @PreRemove: 

Specifies a method to be executed before an entity is removed.

33. @PostRemove: 

Specifies a method to be executed after an entity is removed.

34. @EntityResult: 

Maps the result of a SQL query to an entity or a DTO.

35. @FieldResult: 

Maps a column of a SQL query to a field in an entity or a DTO.

36. @ConstructorResult: 

Maps the result of a SQL query to a constructor of an entity or a DTO.

37. @SqlResultSetMapping: 

Defines the result mapping for a SQL query result set.

38. @Temporal: 

Specifies the temporal type of a date or time field.

39. @Embedded: 

Embeds an object as a component within an entity.

40. @Embeddable: 

Marks a class as an embeddable component.

41. @ElementCollection: 

Maps a collection of simple or embeddable elements.

42. @OrderBy: 

Specifies the order of a collection or an association.

43. @Formula: 

Defines a computed property based on an SQL formula.

44. @AccessType: 

Specifies the access type for an entity or a mapped superclass.

45. @NoRepositoryBean: 

Indicates that a repository interface should not be instantiated as a Spring bean.

46. @Procedure: 

Executes a stored procedure or a database function.

47. @DynamicUpdate: 

Generates update statements dynamically based on the modified properties.

48. @DynamicInsert: 

Generates insert statements dynamically by excluding null properties.

49. @Cacheable: 

Enables caching for an entity or a query result.

50. @CachePut: 

Updates the cache entry for an entity or a query result.

51. @CacheEvict: 

Evicts the cache entry for an entity or a query result.

52. @Where: 

Specifies a runtime filter condition for entities.

53. @Index: 

Configures an index for a database table.

54. @SequenceGenerator: 

Defines a sequence generator for an entity's primary key.

55. @TableGenerator: 

Defines a table generator for an entity's primary key.

56. @AssociationOverride: 

Overrides the mapping of an association in an entity subclass.

57. @AttributeOverride: 

Overrides the mapping of an attribute in an entity subclass.

58. @Converter: 

Specifies a custom attribute converter.

59. @Convert: 

Applies a specified converter to an attribute.

60. @Type: 

Specifies the Hibernate-specific type of an attribute.

61. @Cache: 

Configures the second-level cache settings for an entity.

62. @BatchSize: 

Specifies the batch size for fetching collections.

63. @NamedEntityGraph: 

Defines a named entity graph for a specific entity.

64. @Fetch: 

Specifies the fetching strategy for an association.

65. @LazyCollection: 

Configures the lazy-loading behavior for collections.

66. @FetchProfile: 

Defines a fetch profile to customize fetching behavior.

67. @AuditTable: 

Specifies the audit table for an audited entity.

68. @Audited: 

Enables auditing for an entity.

69. @CreatedBy: 

Tracks the user who created an entity.

70. @LastModifiedBy: 

Tracks the user who last modified an entity.

71. @CreatedDate: 

Tracks the date and time of entity creation.

72. @LastModifiedDate: 

Tracks the date and time of the last entity modification.

73. @LockModeType: 

Specifies the lock mode for a query or method.

74. @ElementJoinColumns: 

Defines join columns for an element collection.

75. @JoinColumnOrFormula: 

Specifies a join column or a formula for an association.

76. @SequenceGenerator: 

Configures a sequence generator for an entity.

77. @CacheConcurrencyStrategy: 

Specifies the cache concurrency strategy for an entity.

78. @NamedQueryHints: 

Specifies query hints for a named query.

79. @SecondaryTable: 

Configures a secondary table for an entity.

80. @SecondaryTables: 

Configures multiple secondary tables for an entity.

81. @IndexColumn: 

Configures an index column for a collection.

82. @Basic: 

Specifies the basic mapping for a persistent property.

83. @AttributeOverrides: 

Overrides the mappings of multiple attributes.

84. @ElementCollectionTable: 

Specifies the table for an element collection.

85. @MapKey: 

Specifies the map key of a map association.

86. @PrePersist: 

Specifies a callback method to be invoked before an entity is persisted.

87. @PostPersist: 

Specifies a callback method to be invoked after an entity is persisted.

88. @PreUpdate: 

Specifies a callback method to be invoked before an entity is updated.

89. @PostUpdate: 

Specifies a callback method to be invoked after an entity is updated.

90. @PreRemove: 

Specifies a callback method to be invoked before an entity is removed.

91. @PostRemove: 

Specifies a callback method to be invoked after an entity is removed.

92. @OrderBy: 

Specifies the order in which a collection or association is sorted.

93. @FetchMode: 

Specifies the fetch mode for an association.

94. @MapKeyColumn: 

Specifies the column to be used as the map key.

95. @NotFound: 

Specifies the action to take when a related entity is not found.

96. @QueryTimeout: 

Specifies the query timeout for a query or method.

97. @CollectionTable: 

Configures the table for a collection.

98. @DiscriminatorColumn: 

Configures the discriminator column for an entity inheritance hierarchy.

99. @DiscriminatorValue: 

Specifies the value for the discriminator column of an entity.

100. @OnDelete: 

Specifies the delete action for a foreign key constraint.


Please note that this is not an exhaustive list, and Spring Data JPA offers even more annotations to cater to various scenarios and requirements.

Post a Comment

Previous Post Next Post