Common annotations

  1. @Entity: Marks a Java class as a JPA entity. Each instance of this class represents a row in the corresponding database table.
  2. @Table: Specifies the details of the database table to which the entity is mapped. This annotation allows you to specify the table name, schema, and other attributes.
  3. @Id: Marks the primary key attribute of the entity. It can be applied to a field or property of the entity class.
  4. @GeneratedValue: Configures the way of generating the primary key values automatically. It's often used in conjunction with the @Id annotation.
  5. @Column: Specifies the details of the database column to which an entity attribute is mapped. This annotation allows you to specify the column name, type, constraints, etc.
  6. @Basic: Defines the default fetch type for an entity attribute. By default, all attributes are fetched eagerly.
  7. @Transient: Indicates that an entity attribute is not persisted to the database. It's useful for fields that should not be stored but are needed in the Java class.
  8. @Temporal: Specifies the type of temporal data (date, time, timestamp) for a persistent attribute. It's often used with java.util.Date or java.time.LocalDateTime fields.
  9. @OneToMany and @ManyToOne: Defines relationships between entities. @OneToMany is used to represent a one-to-many association, while @ManyToOne represents a many-to-one association.
  10. @OneToOne and @JoinColumn: Used to define a one-to-one relationship between entities. @OneToOne is used on the owning side of the relationship, and @JoinColumn is used to specify the column in the owning entity's table that references the related entity's primary key.
  11. @ManyToMany: Indicates a many-to-many relationship between entities. It's typically used in scenarios where each instance of one entity can be related to multiple instances of another entity, and vice versa.

Example:

import javax.persistence.*;

@Entity
@Table(name = "employees")
public class Employee {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "first_name", nullable = false)
    private String firstName;
    
    @Column(name = "last_name", nullable = false)
    private String lastName;
    
    @Temporal(TemporalType.DATE)
    @Column(name = "hire_date")
    private Date hireDate;
    
    @Transient
    private String temporaryData;
    
    @ManyToOne
    @JoinColumn(name = "department_id")
    private Department department;
    
    // Getters and setters omitted for brevity
}

@Entity
@Table(name = "departments")
public class Department {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(name = "name", nullable = false)
    private String name;
    
    @OneToMany(mappedBy = "department")
    private List<Employee> employees;
    
    // Getters and setters omitted for brevity
}

In this example:

  • @Entity, @Table, @Id, @GeneratedValue, @Column, @Temporal, and @Transient annotations are used in the Employee class.
  • @ManyToOne and @JoinColumn annotations are used to establish a many-to-one relationship between Employee and Department entities.
  • @OneToMany annotation in the Department class establishes a one-to-many relationship with Employee entities, specifying the mappedBy attribute to refer to the field in the Employee class that owns the relationship.