我真的是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;
}
}
我在哪里误会? 感谢您的帮助。
编辑:我添加了导入
答案 0 :(得分:-1)
将Entity class'包添加到@ComponentScan,即
@ComponentScan({"org.html.topolino.model"})
应该可以。