0

I'm really new to JPA and ORMs, so I hope you'll pardon my silly question. I read many posts on this blog and elsewhere on the web, but suggested solution, even if they seem reasonable, didn't work for me.

I have a well known exception:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'issueService': Unsatisfied dependency expressed through field 'issueRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'issueRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class org.html.topolino.model.Issue

Since it seemed a matter of visibility, i modified my SpringBootApplication as suggested on the web:

 package org.html.topolino;

import org.apache.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
//@EntityScan(basePackages = {"org.html.topolino.model"})
//@EnableJpaRepositories(basePackages = {"org.html.topolino.repositories"})
//@ComponentScan({"org.html.topolino.services", "org.html.topolino.controllers", "org.html.topolino.repositories", "org.html.topolino.model"})
public class TopolinoApplication 
{
    static Logger logger = Logger.getLogger(TopolinoApplication.class.getName());

    public static void main(String[] args) 
    {
        logger.info("Starting TopolinoApplication...");
        SpringApplication.run(TopolinoApplication.class, args);
        logger.info("Started TopolinoApplication");
    }

}

This is my controller:

package org.html.topolino.controllers;

import java.io.IOException;
import java.text.ParseException;

import org.html.topolino.model.Issue;
import org.html.topolino.parser.HTMLParser;
import org.html.topolino.services.IssueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/topolinoRest")
public class IssueController 
{
    @Autowired
    private IssueService issueService;

    @RequestMapping(value = "/saveAllIssues/", method = RequestMethod.GET)
    public void saveAllIssues()
    {
        /* mapping dell'HTML nel Document */
        Integer issueNumber = 1210;

        try 
        {
            Issue issue = HTMLParser.getIssueData(issueNumber);
            issueService.addIssue(issue);
        } 

        catch (ParseException | IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

My service:

 package org.html.topolino.services;

import org.html.topolino.model.Issue;
import org.html.topolino.repositories.IssueRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.stereotype.Service;

@Service
public class IssueService 
{
    @Autowired
    private IssueRepository issueRepository;

    public void addIssue(Issue issue)
    {
        issueRepository.save(issue);
    }
}

My repository:

package org.html.topolino.repositories;

import org.html.topolino.model.Issue;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface IssueRepository extends CrudRepository<Issue, Integer> 
{
//  public Optional<Issue> findById(Integer id);
}

Finally, the entity which is not found:

package org.html.topolino.model;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity(name = "Issue")
@Table(name = "Issue")
public class Issue implements Serializable
{
    /**
     * 
     */
    private static final long serialVersionUID = 4829348412099252089L;

//  @Id 
//  @GeneratedValue 
//  long id;
//  
//  @Column
    @Id
    private Integer progressiveNumber;

    @ElementCollection
    private List<String> publishers;

    @Column
    private Date publishingDate;

    @OneToOne(mappedBy = "issue", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
    private Cover cover;

    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
    @JoinColumn(name = "issue_stories")
    private List<Story> listOfStories;

    public Issue() {
        super();
    }

    public Integer getProgressiveNumber() {
        return progressiveNumber;
    }

    public void setProgressiveNumber(Integer progressiveNumber) {
        this.progressiveNumber = progressiveNumber;
    }

    public List<String> getPublishers() {
        return publishers;
    }

    public void setPublishers(List<String> publishers) {
        this.publishers = publishers;
    }

    public Date getPublishingDate() {
        return publishingDate;
    }

    public void setPublishingDate(Date publishingDate) {
        this.publishingDate = publishingDate;
    }

    public Cover getCover() {
        return cover;
    }

    public void setCover(Cover cover) {
        this.cover = cover;
    }

    public List<Story> getListOfStories() {
        return listOfStories;
    }

    public void setListOfStories(List<Story> listOfStories) {
        this.listOfStories = listOfStories;
    }



}

Where an I mistaking? Thanks for help.

EDIT: I've added imports

Bia
  • 165
  • 1
  • 3
  • 12
  • Hi. This is one of solutions I found before deciding to write this post. Sadly it didn't work. Maybe I'm mistaking, but according to what I've learnt from pages I've read, ComponentScan is used to scan for controllers, services and repository, whose main annotation is a "subclass" of ComponentScan – Bia Dec 17 '19 at 11:30
  • Did you try to remove these three annotations from your application class? @EntityScan(basePackages = {"org.html.topolino.model"}) @EnableJpaRepositories(basePackages = {"org.html.topolino.repositories"}) @ComponentScan({"org.html.topolino.services", "org.html.topolino.controllers", "org.html.topolino.repositories"}) – Hatice Dec 17 '19 at 11:34
  • Thanks for you answer. I've tried this kind of solution, it was my starting point before getting exception – Bia Dec 17 '19 at 11:37
  • 1
    @Bia Can you show the imports? – Jens Dec 17 '19 at 11:39
  • Why did you comment out id field in Issue class? You are looking for id field in findById method. – Hatice Dec 17 '19 at 11:40
  • Follow the suggestions of the Spring Boot team. Move your `TopolinoApplication` to the `org.html.topolino` package. Remove all annotations but `@SpringBootApplication` and restart your application. Also remove the `findById` that is already provided by Spring Data JPA. – M. Deinum Dec 17 '19 at 11:43
  • My application is already in `org.html.topolino`. I removed method in repository (it was just a kind of desperate attempt) and left only `@SpringBootApplication`. Still no solution. – Bia Dec 17 '19 at 11:50

1 Answers1

-1

Add Entity class' package to @ComponentScan,i.e.

@ComponentScan({"org.html.topolino.model"})

It should work.

New Bee
  • 390
  • 3
  • 10
  • Thanks for your answer. I've added the package containing entity to the ones in ComponentScan. I've removed EntityScan, but I still have same problem. I've made an attempt keeping ComponentScan and EntityScan at same time. Nothing has changed. – Bia Dec 17 '19 at 11:35