当前位置: 首页 > 知识库问答 >
问题:

JPA:找不到具有@EntityScan[重复]的实体

穆承运
2023-03-14

我对JPA和ORMs真的很陌生,所以我希望你原谅我的愚蠢问题。我在这个博客和其他网站上读了很多帖子,但建议的解决方案,即使看起来合理,对我来说并不奏效。

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



}

我哪里搞错了?多谢帮忙。

编辑:我已添加导入

共有1个答案

陈成济
2023-03-14

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

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

应该管用。

 类似资料:
  • 有两个表。酒店内的地址。我已经提到了OneTo很多关系。但是编译器抛出错误。 创建名为entityManagerFactory的bean时出错,该bean在类路径资源[org/springFramework/boot/autoconfiure/orm/jpa/HibernateJpaConfiguration.class]中定义:调用init方法失败;嵌套异常org.hibernate.Mappi

  • Contact实体定义了与两个MySQL表中存在的email和nickname类型的实体集合的关系。 我的问题是返回的结果集有重复的电子邮件和昵称。 当重复发生时,Hibernate正在运行以下操作。 问候你,伊恩。

  • 问题内容: 在我们的hibernate项目中,使用java bean模式对实体进行编码。在我们的代码中,有很多地方有人忘记了设置变量的设置,但由于NOT NULL字段而导致异常。 是否有人在使用构建器来构建其实体或使其不可变? 我正在尝试找到一种不是Java bean模式样式的有效模式。 谢谢 问题答案: 如果使Bean不可变,则必须使用字段级访问,并且这附带了它自己的一系列问题,在此进行了详细讨

  • 我是一名学习使用jsp和Servlet构建Web应用程序的学生。一个月以来,我的Web应用程序项目一直运行良好,但今天它的行为突然变得奇怪了。当我提交jsp页面时,它无法找到我的servlet。我已经使用servlet注释来映射请求。 以下是我的JSP:- 以下是我的servlet:- 以下是我的控制台日志:-

  • 我有一个实体,它的复合主键由两个字段组成,其中一个也是复合外键的一部分。 背景:我有实体<代码>人员 、<代码>区域 和<代码>会话 。 与具有多对多关系,使用称为“和实体。 所以,我有,主键为(,)。本身是的外键。 也有一个字段。我希望(,)是的复合外键。 我的PersonSession代码: } 这看起来不错,它在数据库中创建了所有正确的关系。当我尝试插入个性化会话对象时,问题就出现了——ar

  • 我在尝试将log4j2与Spring Boot一起使用时遇到此错误。 我已经遵循了这个指南:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-log4j-for-logging-并且还添加了两个log4j2依赖于https://logging.apache