Author

Overview

In the context of the book "Spring Boot in Practice," the Author entity is a key component of a many-to-many relationship with the Course entity. This relationship is crucial for managing the association between authors and the courses they create or contribute to. The Author entity is represented in the database by the AUTHOR table, and its relationship with courses is managed through a join table named AUTHORS_COURSES.

The Author Entity

The Author entity is defined in Java as follows:

package com.manning.sbip.ch03.model;
 
@Entity(name = "AUTHOR")
@Table(name="AUTHORS")
public class Author {
 
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    private String bio;
    @ManyToMany
    @JoinTable(name = "AUTHORS_COURSES",
            joinColumns = {
      @JoinColumn(name="author_id", 
➥ referencedColumnName = "id", nullable = false, 
➥ updatable = false)},
            inverseJoinColumns = {
      @JoinColumn(name="course_id", 
➥ referencedColumnName = "id", nullable = false, 
➥ updatable = false)}
    )
    private Set<Course> courses = new HashSet<>();
 
    public Author() {}
 
    public Author(String name, String bio) {
        this.name = name;
        this.bio = bio;
    }
    public long getId() {
        return id;
    }
 
    public String getName() {
        return name;
    }
 
    public String getBio() {
        return bio;
    }
 
    public Set<Course> getCourses() {
        return courses;
    }
 
    @Override
    public String toString() {
        return "Author{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", bio='" + bio + '\'' +
                '}';
    }
}

Key Annotations

  • @Entity and @Table: These annotations specify that the class is an entity and is mapped to the AUTHORS table in the database.
  • @Id and @GeneratedValue: These annotations define the primary key of the entity and specify that its value is automatically generated.
  • @ManyToMany and @JoinTable: These annotations define the many-to-many relationship between Author and Course, using the AUTHORS_COURSES join table to manage the relationship.

Database Schema

The database schema for the Author and Course entities, along with their relationship, is defined as follows:

Listing 3.40 The Schema.sql

CREATE TABLE authors (
  id   BIGINT NOT NULL,
  bio  VARCHAR(255),
  name VARCHAR(255),
  PRIMARY KEY (id)
);
 
CREATE TABLE authors_courses (
  author_id BIGINT NOT NULL,
  course_id BIGINT NOT NULL,
  PRIMARY KEY (author_id, course_id)
);
 
CREATE TABLE courses (
  id          BIGINT NOT NULL,
  category    VARCHAR(255),
  description VARCHAR(255),
  name        VARCHAR(255),
  rating      INTEGER NOT NULL,
  PRIMARY KEY (id)
);
 
ALTER TABLE authors_courses
  ADD CONSTRAINT course_id_fk FOREIGN KEY
➥ (course_id) REFERENCES courses (id);
 
ALTER TABLE authors_courses
  ADD CONSTRAINT author_id_fk FOREIGN KEY (author_id)
➥ REFERENCES authors (id);

Data Insertion

The following SQL script inserts sample data into the AUTHORS, COURSES, and AUTHORS_COURSES tables:

Listing 3.41 The data.sql script

INSERT INTO COURSES(ID, NAME, CATEGORY, RATING, DESCRIPTION)
➥ VALUES(1, 'Rapid Spring Boot Application Development',
➥ 'Spring', 4, 'Spring Boot gives all the power of the
➥ Spring Framework without all of the complexity');
INSERT INTO COURSES(ID, NAME, CATEGORY, RATING, DESCRIPTION)
➥ VALUES(2, 'Getting Started with Spring Security DSL',
➥ 'Spring', 5, 'Learn Spring Security DSL in easy steps');
INSERT INTO COURSES(ID, NAME, CATEGORY, RATING, DESCRIPTION)
➥ VALUES(3, 'Getting Started with Spring Cloud Kubernetes',
➥ 'Python', 3, 'Master Spring Boot application deployment
➥ with Kubernetes');
INSERT INTO AUTHORS(ID, NAME, BIO)
➥ VALUES(1, 'John Doe',
➥ 'Author of several Spring Boot courses');
INSERT INTO AUTHORS(ID, NAME, BIO)
➥ VALUES(2, 'Steve Muller', 'Author of several
➥ popular Spring and Python courses');
INSERT INTO AUTHORS_COURSES(AUTHOR_ID, COURSE_ID) VALUES(1, 1);
INSERT INTO AUTHORS_COURSES(AUTHOR_ID, COURSE_ID) VALUES(1, 2);
INSERT INTO AUTHORS_COURSES(AUTHOR_ID, COURSE_ID) VALUES(2, 1);
INSERT INTO AUTHORS_COURSES(AUTHOR_ID, COURSE_ID) VALUES(2, 2);
INSERT INTO AUTHORS_COURSES(AUTHOR_ID, COURSE_ID) VALUES(2, 3);

Entity-Relationship Diagram

The relationship between the Author and Course entities is depicted in the following entity-relationship diagram:

Figure 3.7 Author and Course entity–relationship diagram. In an ER diagram, the relationship table is represented by the relationship arrow itself. Thus, the relationship table is not present in the diagram. Figure 3.7 Author and Course entity–relationship diagram. In an ER diagram, the relationship table is represented by the relationship arrow itself. Thus, the relationship table is not present in the diagram.

Author Repository

The AuthorRepository interface extends the CrudRepository to provide basic CRUD operations and includes a custom finder method to retrieve course details authored by a specific author. This is achieved using a JPQL query that joins the AUTHORS, COURSES, and AUTHORS_COURSES tables.

Figure 3.8 Author, Course, and Authors_Courses tables with AuthorCourseDto POJO Figure 3.8 Author, Course, and Authors_Courses tables with AuthorCourseDto POJO