Here are some of the key annotations used in Spring Data JPA and their purposes:
1. @Entity
- Purpose: Marks a class as a JPA entity, indicating that it should be mapped to a table in a database.
- Example:
java
@Entity
public class User {
// Class body
}
2. @Id
- Purpose: Specifies the primary key of an entity. This annotation is used to mark a field as the unique identifier for the entity.
- Example:
java
@Id
private Long id;
3. @GeneratedValue
- Purpose: Specifies the generation strategy for the primary key. It is often used in combination with the
@Id
annotation to define how the primary key should be generated (e.g., automatically by the database). - Example:
java
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
4. @Table
- Purpose: Specifies the table in the database that the entity is mapped to. This annotation is optional and is used if the table name in the database differs from the entity class name.
- Example:
java
@Entity
@Table(name = "users")
public class User {
// Class body
}
5. @Column
- Purpose: Specifies the details of the column to which a field is mapped. It can define the column name, length, nullable status, and more.
- Example:
java
@Column(name = "user_name", nullable = false, length = 100)
private String name;
6. @Repository
- Purpose: Marks a class as a Spring Data repository, which is responsible for performing CRUD operations on entities. It is a specialization of the
@Component
annotation, allowing for automatic exception translation. - Example:
java
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
// Repository methods
}
7. @Query
- Purpose: Defines a custom JPQL or SQL query directly on the repository method. It is used when the query cannot be automatically generated by Spring Data JPA.
- Example:
java
@Query("SELECT u FROM User u WHERE u.email = ?1")
User findByEmail(String email);
8. @Transient
- Purpose: Specifies that a field is not to be persisted in the database. This annotation is used for fields that are not mapped to any database column.
- Example:
java
@Transient
private int temporaryValue;
9. @ManyToOne
- Purpose: Defines a many-to-one relationship between two entities. It is used to specify the association between entities and how they should be mapped to database tables.
- Example:
java
@ManyToOne
@JoinColumn(name = "department_id")
private Department department;
10. @OneToMany
- Purpose: Defines a one-to-many relationship between two entities. It is used when one entity is related to multiple instances of another entity.
- Example:
java
@OneToMany(mappedBy = "department")
private List<Employee> employees;
11. @ManyToMany
- Purpose: Specifies a many-to-many relationship between two entities. This annotation is often used with a
@JoinTable
annotation to define the join table for the relationship. - Example:
java
@ManyToMany
@JoinTable(
name = "employee_project",
joinColumns = @JoinColumn(name = "employee_id"),
inverseJoinColumns = @JoinColumn(name = "project_id")
)
private Set<Project> projects;
12. @OneToOne
- Purpose: Defines a one-to-one relationship between two entities. It is used when each instance of an entity is associated with a unique instance of another entity.
- Example:
java
@OneToOne
@JoinColumn(name = "passport_id")
private Passport passport;