【问题标题】:JPA: can't find entity with @EntityScan [duplicate]JPA:找不到带有@EntityScan的实体[重复]
【发布时间】:2020-04-09 21:24:53
【问题描述】:

我对 JPA 和 ORM 真的很陌生,所以我希望你能原谅我的愚蠢问题。 我在这个博客和网络上的其他地方阅读了许多帖子,但建议的解决方案,即使它们看起来很合理,对我也不起作用。

我有一个众所周知的例外:

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

由于这似乎是一个可见性问题,我按照网上的建议修改了我的 SpringBootApplication:

 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");
    }

}

这是我的控制器:

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();
        }
    }

}

我的服务:

 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);
    }
}

我的仓库:

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);
}

最后是找不到的实体:

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;
    }



}

我在哪里搞错了? 感谢您的帮助。

编辑:我添加了导入

【问题讨论】:

  • 嗨。这是我在决定写这篇文章之前找到的解决方案之一。可悲的是它没有用。也许我弄错了,但是根据我从我读过的页面中了解到的,ComponentScan 用于扫描控制器、服务和存储库,其主要注释是 ComponentScan 的“子类”
  • 您是否尝试从您的应用程序类中删除这三个注释? @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"})
  • 感谢您的回答。我已经尝试过这种解决方案,这是我在获得异常之前的起点
  • @Bia 你能展示进口吗?
  • 为什么在Issue类中注释掉id字段?您正在 findById 方法中查找 id 字段。

标签: java spring jpa


【解决方案1】:

将实体类的包添加到@ComponentScan,即

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

它应该可以工作。

【讨论】:

  • 感谢您的回答。我已将包含实体的包添加到 ComponentScan 中的包中。我已经删除了 EntityScan,但我仍然有同样的问题。我尝试同时保持 ComponentScan 和 EntityScan。什么都没有改变。
猜你喜欢
  • 2021-01-07
  • 1970-01-01
  • 1970-01-01
  • 2015-07-10
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 2019-06-29
  • 2018-04-09
相关资源
最近更新 更多