0

I have a mysql database and I added two entries in the table TvSeries. But I can't retrieve the the entries. Below are my Model Class, my Repository Class, my Service Class and the main method where I've tried to retrieve the information.

import javax.persistence.*;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Entity
public class TvSeries {
    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    private Integer id;

    public Integer getId() {
        return id;
    }

    @NotNull
    @NotEmpty
    private String title = "";

    private String description = "";

    @NotNull
    @NotEmpty
    private int noOfSeasons;

    public String getTitle() {
        return title;
    }

    public String getDescription() {
        return description;
    }

    public int getNoOfSeasons() {
        return noOfSeasons;
    }

    @ManyToMany(mappedBy = "watchedSeries")
    Set<User> watches;

    @OneToMany(mappedBy = "tvSeries")
    List<Episode> episodes = new ArrayList<>();

    @Override
    public String toString() {
        return "TvSeries{" +
                "id=" + id +
                ", title='" + title + '\'' +
                ", description='" + description + '\'' +
                ", noOfSeasons=" + noOfSeasons +
                '}';
    }
}
import com.example.tvseriestrackingwebapp.backend.models.TvSeries;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TvSeriesRepository extends JpaRepository<TvSeries, Integer> {
}
import com.example.tvseriestrackingwebapp.backend.models.TvSeries;
import com.example.tvseriestrackingwebapp.backend.models.User;
import com.example.tvseriestrackingwebapp.backend.repositories.TvSeriesRepository;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TVSeriesService {
    private TvSeriesRepository tvSeriesRepository;

    public List<TvSeries> findAll() {
        return tvSeriesRepository.findAll();
    }

}

public class TvSeriesTrackingWebApplication {

    public static void main(String[] args) {

        SpringApplication.run(TvSeriesTrackingWebApplication.class, args);
        TVSeriesService tvSeriesService = new TVSeriesService();
        Iterator itr = tvSeriesService.findAll().iterator();
        while (itr.hasNext()) {
            TvSeries tvSeries = (TvSeries) itr.next();
            System.out.println(tvSeries);
        }
    }

}

Also, I'm getting the java.lang.NullPointerException error and I think it's because there aren't any elements in the table.

Diana
  • 363
  • 2
  • 8
  • @Kartik Yeah, it does. Thank you. However, I now have a new question. Is there any way to test that the application retrieves data from the MySQL application? In the main function I mean – Diana May 13 '20 at 00:17
  • Also annotate you entities with "@Table" – Eric May 13 '20 at 17:59

0 Answers0