Spring Boot Interview Questions

25 Questions
Spring Boot

Spring Boot

Java

Java

BackendWeb Development

Question 16

Explain the role of the @Entity annotation.

Answer:

The @Entity annotation is a part of the Java Persistence API (JPA) and is used to specify that a class is an entity. An entity represents a table in a relational database, and each instance of the entity corresponds to a row in that table. The @Entity annotation is crucial for enabling the ORM (Object-Relational Mapping) capabilities provided by JPA and frameworks like Hibernate, which is often used as the JPA implementation.

Role and Features of the @Entity Annotation

  1. Marks a Class as an Entity:

    • The primary role of the @Entity annotation is to mark a Java class as an entity, which will be managed by the JPA provider (such as Hibernate).
    • This annotation informs the JPA provider that the class should be mapped to a table in the database.
  2. Mapping to Database Table:

    • By default, the name of the table is the same as the class name. However, you can customize it using the @Table annotation.
    • The entity class fields are mapped to columns in the database table. Each field represents a column, and the field values represent the data stored in those columns.
  3. Primary Key:

    • Every entity must have a primary key, which is specified using the @Id annotation.
    • The primary key can be a single field or a composite key (using multiple fields).
  4. Annotations for Field Mapping:

    • Additional JPA annotations can be used to customize the mapping of entity fields to database columns. These include @Column, @Temporal, @Enumerated, etc.
    • You can also specify relationships between entities using annotations like @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany.

Example of an Entity Class

Here is an example of how to define an entity class using the @Entity annotation, along with other relevant annotations to map the class to a database table.

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Column;

@Entity
@Table(name = "users")
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "username", nullable = false, unique = true)
    private String username;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "email", nullable = false, unique = true)
    private String email;

    // Constructors, getters, and setters

    public User() {
    }

    public User(String username, String password, String email) {
        this.username = username;
        this.password = password;
        this.email = email;
    }

    // Getters and setters
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

Breakdown of Annotations

  • @Entity:
    • Marks the class as a JPA entity.
  • @Table:
    • Specifies the name of the database table to which the entity is mapped. If omitted, the table name defaults to the class name.
  • @Id:
    • Specifies the primary key of the entity.
  • @GeneratedValue:
    • Defines the strategy for primary key generation. GenerationType.IDENTITY indicates that the database will generate the primary key.
  • @Column:
    • Customizes the mapping between the field and the database column. Attributes like name, nullable, and unique are used to define the column properties.

Relationships Between Entities

Entities often have relationships with other entities. JPA provides annotations to define these relationships.

Example of a One-to-Many Relationship

import javax.persistence.*;
import java.util.List;

@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", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Employee> employees;

    // Constructors, getters, and setters
}

@Entity
@Table(name = "employees")
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name", nullable = false)
    private String name;

    @ManyToOne
    @JoinColumn(name = "department_id", nullable = false)
    private Department department;

    // Constructors, getters, and setters
}

Summary

  • @Entity: Marks a class as a JPA entity to be managed by the JPA provider.
  • @Table: Specifies the table in the database to which the entity is mapped.
  • @Id: Specifies the primary key of the entity.
  • @GeneratedValue: Defines how the primary key should be generated.
  • @Column: Customizes the mapping of entity fields to database columns.
  • Relationship Annotations: Such as @OneToOne, @OneToMany, @ManyToOne, and @ManyToMany, to define relationships between entities.

The @Entity annotation and its related JPA annotations are essential for mapping Java classes to database tables and enabling the powerful ORM features provided by JPA.

Recent job openings