我在运行期间遇到错误(为简洁省略了完整目录:
创建名称为' idea_Service'的bean时出错在文件中定义 [... COM \ vincentsnow \ brightideas \ SERVICES \ Idea_Service.class]: 通过构造函数参数0表示的不满意的依赖性; 嵌套异常是 org.springframework.beans.factory.BeanCreationException:错误 创建名为' idea_Repository'的bean:调用init方法 失败;嵌套异常是java.lang.IllegalArgumentException:不是 托管类型:类java.lang.Object
和另一个:
使用名称' idea_Repository创建bean时出错':调用init 方法失败;嵌套异常是java.lang.IllegalArgumentException: 不是托管类型:class java.lang.Object
我还没有在任何地方使用这些小写字母。我无法理解这个消息......我猜测它正在尝试编译,但出于某种原因改变了文件名。我的文件中没有STS捕获错误。他们在这里:
Idea_Service.java(服务)
package com.vincentsnow.brightideas.services;
import java.util.List;
import org.springframework.stereotype.Service;
import com.vincentsnow.brightideas.models.Idea;
import com.vincentsnow.brightideas.models.User;
import com.vincentsnow.brightideas.repositories.Idea_Repository;
@Service
public class Idea_Service {
private Idea_Repository ideaRepository;
public Idea_Service(Idea_Repository ideaRepository){
this.ideaRepository = ideaRepository;
}
public List<Idea> allIdeas(){
return ideaRepository.findAllIdeas();
}
public void createUser(String idea, User posted_by){
ideaRepository.save(idea, posted_by);
}
public Idea oneIdea(Long id) {
return ideaRepository.getSingleIdeaWhereId(id);
}
public List<Idea> likesof(Long id){
return ideaRepository.getLikesOfIdea(id);
}
}
Idea.java(模特)
package com.vincentsnow.brightideas.models;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.validation.constraints.Size;
import com.vincentsnow.brightideas.models.User;
@Entity
public class Idea {
@Id
@GeneratedValue
private Long id;
@Column
@Size(min=3, max=5000)
private String idea;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="idea_id")
private User posted_by;
@ManyToMany(fetch = FetchType.LAZY)
@JoinTable(
name = "likes",
joinColumns = @JoinColumn(name = "idea_id"),
inverseJoinColumns = @JoinColumn(name = "user_id")
)
private List<Idea> likes;
}
Idea_Repository.java(存储库)
package com.vincentsnow.brightideas.repositories;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;
import com.vincentsnow.brightideas.models.Idea;
import com.vincentsnow.brightideas.models.User;
@Repository
public interface Idea_Repository extends CrudRepository {
@Query("SELECT a FROM ideas a")
List<Idea> findAllIdeas();
@Query(value="INSERT INTO ideas (idea, posted_by) VALUES (?1, ?2, ?3, ?4)", nativeQuery=true)
Idea save(String idea, User posted_by);
@Query("SELECT a FROM ideas a WHERE id=?1")
Idea getSingleIdeaWhereId(Long id);
@Query(value="SELECT likes WHERE idea_id = ?1", nativeQuery=true)
List<Idea> getLikesOfIdea(Long id);
}
他们有什么问题?这个错误是什么意思? STS试图做什么?
答案 0 :(得分:2)
CrudRepository
有两个形式参数,即它管理的类型和该类型ID的类型。由于您还没有提供这些参数,因此它们实际上是Object
和Object
:错误消息指出Object
不是可管理的对象(它是&#39;没有用@Entity
注释。您需要告诉Spring Data您的存储库实际尝试管理的类是Idea
。尝试:
public interface Idea_Repository extends CrudRepository<Idea, Long> {
// ..
}
此外,您不需要findAllIdeas
,save
或getSingleIdeaWhereId
; Spring Data JPA免费提供等效方法。