Citation and Coment entities

Citation and Coment entities

Entities

In Spring Boot, an entity represents a persistent object in a database, and it is typically defined as a Java class with JPA annotations. The JPA annotations define the mapping between the entity class and the corresponding database table, as well as the relationships between entities.

Entities in Spring Boot are typically defined as POJOs (Plain Old Java Objects) and have instance variables that correspond to columns in the database table. In addition, entities often have relationships to other entities, such as one-to-one, one-to-many, or many-to-many relationships.

To define an entity in Spring Boot, you typically annotate the class with @Entity, and annotate each instance variable with the appropriate JPA annotations, such as @Id for the primary key, @Column for a column in the table, and @OneToMany or @ManyToOne for relationships with other entities.

Spring Boot provides several ways to work with entities, such as using the JPA EntityManager, the CrudRepository interface, or the JpaRepository interface. These interfaces provide methods for CRUD (Create, Read, Update, Delete) operations on entities, as well as querying for entities using various criteria.

Entities are a core concept in Spring Boot and are essential for creating robust and scalable database-driven applications. Properly defining entities and their relationships is critical for creating a well-designed database schema and ensuring efficient data access and retrieval.

Citation

import jakarta.persistence.Entity;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.List;

@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
@Builder
public class Citation {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private  Long id;
    private String content;
    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "citation_id")
    private List<Comment> comments= new ArrayList<>();
}

This code defines a Java class called Citation, which is annotated with JPA (Java Persistence API) annotations to map it to a database table. The class has several instance variables, including an id of type Long, a content of type String, and a List of Comment objects called comments. The @Id and @GeneratedValue annotations specify that the id field is the primary key and should be automatically generated by the database.

The @OneToMany and @JoinColumn annotations specify a one-to-many relationship between Citation and Comment entities, with the citation_id column in the Comment table being the foreign key that links back to the Citation table.

The @AllArgsConstructor, @NoArgsConstructor, @Data, and @Builder annotations are part of the Lombok library, which generates constructors, getter/setter methods, and a builder method for the class at compile time, based on the annotations used.

Comment

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class Comment {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String  body;
}

This code defines a Java class called Comment, which is annotated with JPA annotations to map it to a database table. The class has two instance variables, an id of type Long, and a body of type String. The @Id and @GeneratedValue annotations specify that the id field is the primary key and should be automatically generated by the database.

The @AllArgsConstructor, @NoArgsConstructor, and @Data annotations are part of the Lombok library, which generates constructors, getter/setter methods, and other utility methods for the class at compile time, based on the annotations used.

The @Entity annotation marks the class as an entity that can be mapped to a database table, and it is necessary to use the @Data annotation to generate the necessary boilerplate code for a JPA entity class.

Conclusion

In this article, we discussed the creation of entities. This creation was facilitated by the use of Lombok annotations, and we also materialized the different relationships between the two entities. In the next article, we will cover the creation of repositories