Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA:can';找不到具有@EntityScan的实体_Java_Spring_Jpa - Fatal编程技术网

Java JPA:can';找不到具有@EntityScan的实体

Java JPA:can';找不到具有@EntityScan的实体,java,spring,jpa,Java,Spring,Jpa,我对JPA和ORMs真的很陌生,所以我希望你能原谅我这个愚蠢的问题。 我在这个博客和其他网站上读了很多帖子,但建议的解决方案,即使看起来合理,对我来说也不起作用 我有一个众所周知的例外: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'issueService': Unsatisfied dependency expressed through fie

我对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
由于这似乎是一个可视性问题,我按照web上的建议修改了我的SpringBoot应用程序:

 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.repositories;
导入org.html.topolino.model.Issue;
导入org.springframework.data.repository.crudepository;
导入org.springframework.stereotype.Repository;
@存储库
公共接口IssueRepository扩展了CrudePository
{
//公共可选findById(整数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;
    }



}
package org.html.topolino.model;
导入java.io.Serializable;
导入java.util.Date;
导入java.util.List;
导入javax.persistence.CascadeType;
导入javax.persistence.Column;
导入javax.persistence.ElementCollection;
导入javax.persistence.Entity;
导入javax.persistence.FetchType;
导入javax.persistence.GeneratedValue;
导入javax.persistence.Id;
导入javax.persistence.JoinColumn;
导入javax.persistence.OneToMany;
导入javax.persistence.OneToOne;
导入javax.persistence.Table;
@实体(名称=“发行”)
@表(name=“问题”)
公共类问题实现了可序列化
{
/**
* 
*/
私有静态最终长serialVersionUID=4829348412099252089L;
//@Id
//@GeneratedValue
//长id;
//  
//@列
@身份证
私有整数累进数;
@元素集合
私人名单出版商;
@纵队
私人日期发布日期;
@OneTONE(mappedBy=“issue”,cascade=CascadeType.ALL,fetch=FetchType.LAZY,可选=false)
私人保险;
@OneToMany(cascade=CascadeType.ALL,orphan=true)
@JoinColumn(name=“发布故事”)
私有目录;
公开发行(){
超级();
}
公共整数getProgressiveNumber(){
返回累进数;
}
public void setProgressiveNumber(整数progressiveNumber){
this.progressiveNumber=progressiveNumber;
}
公共列表getpublisher(){
归还出版商;
}
公共发布者(列出发布者){
this.publisher=出版商;
}
公共日期getPublishingDate(){
返回发布日期;
}
公共作废设置发布日期(日期发布日期){
this.publishingDate=publishingDate;
}
公众封面{
返回盖;
}
公共空白封面(封面){
this.cover=cover;
}
公共列表getListOfStories(){
返回目录;
}
公共作废集合列表(列表列表列表){
this.listOfStories=listOfStories;
}
}
我错在哪里了? 谢谢你的帮助


编辑:我已将导入添加到@ComponentScan的“添加实体类”包中,即

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

它应该能工作。

嗨。这是我在决定写这篇文章之前找到的解决方案之一。遗憾的是,它没有起作用。也许我弄错了,但根据我从阅读的页面中了解到的,ComponentScan用于扫描控制器、服务和存储库,其主要注释是ComponentScanDid的“子类”,您是否尝试从应用程序类中删除这三个注释@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”})谢谢您的回答。我曾经尝试过这种解决方案,这是我在获得成功之前的出发点exception@Bia您能显示导入内容吗?为什么在Issue类中注释掉id字段?您正在findById方法中查找id字段。谢谢您的回答。我已经将包含实体的包添加到ComponentScan中的包中。我已经删除了EntityScan,但仍然有相同的问题。我试图同时保持ComponentScan和EntityScan。一切都没有改变。