Spring UnsatifiedPendencyException且不是托管类型:类java.lang.Object
我想为两个域对象创建一个通用存储库类: 产品和类别,并能够列出index.html中的所有元素 这是我的密码:Spring UnsatifiedPendencyException且不是托管类型:类java.lang.Object,java,spring,java.lang,Java,Spring,Java.lang,我想为两个域对象创建一个通用存储库类: 产品和类别,并能够列出index.html中的所有元素 这是我的密码: @Controller public class IndexController { private final ProdCatService<Category> categoryProdCatService; public IndexController(ProdCatService<Category> categoryProdCatService) {
@Controller
public class IndexController {
private final ProdCatService<Category> categoryProdCatService;
public IndexController(ProdCatService<Category> categoryProdCatService) {
this.categoryProdCatService = categoryProdCatService;
}
@RequestMapping({"","/","index"})
public String getIndexPage(Model model)
{
model.addAttribute("cat", categoryProdCatService.getAll());
return "index";
}
}
及
我请求帮助。我看了一遍S/O,但找不到答案
编辑类别域类
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String categoryName;
@ManyToMany(mappedBy = "categories")
private Set<Product> products;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Category other = (Category) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
@实体
公共类类别{
@身份证
@GeneratedValue(策略=GenerationType.IDENTITY)
私人长id;
私有字符串categoryName;
@许多(mappedBy=“类别”)
私人套装产品;
@凌驾
公共int hashCode(){
最终整数素数=31;
int结果=1;
result=prime*result+((id==null)?0:id.hashCode();
返回结果;
}
@凌驾
公共布尔等于(对象obj){
if(this==obj)
返回true;
if(obj==null)
返回false;
如果(getClass()!=obj.getClass())
返回false;
类别其他=(类别)obj;
if(id==null){
if(other.id!=null)
返回false;
}如果(!id.equals(other.id))
返回false;
返回true;
}
我想为两个域对象创建一个通用存储库类:
产品和类别,并能够列出index.html中的所有元素
你的要求毫无意义。要实现它,您应该在产品和类别之间定义一个基类,并在通用存储库中指定该基类。
类别是与产品不同的概念。
因此,通过扭曲层次结构,您将无法定义此基类。
即使您定义了它,它也不会反映类别和产品中所需的字段。
那么,在这些情况下,您希望如何在客户端正确地列出它们呢?
那么,你为什么要重新发明轮子呢?
实际上,您使用弹簧数据来减少锅炉板代码:
public interface ProdCatRepository<T> extends CrudRepository<T, Long> { }
public interface ProdCatRepository扩展了crudepository{}
它足够清晰,易于申报。不要试图通过关联不应该关联的内容来显得太聪明,并为每个内容定义一个不同的存储库:
public interface ProductRepository extends CrudRepository<Product, Long> { }
public interface CategoryRepository extends CrudRepository<Category, Long> { }
public interface ProductRepository扩展了crudepository{}
公共接口类别repository扩展了crudepository{}
首先从这些服务/存储库属性中删除final
修饰符。bean定义在哪里,根据错误,在创建beanfinal时似乎没有传递正确的参数,仍然得到erroruser2044822,哪个bean?Category bean。在最后一行中,您指的是公共接口CategoryRepository扩展Crudepository{}?好吧,如果你认为这是一个比使用泛型更好、更干净的选择,我会按照你的方式来完成。@janusz如果泛型不是更好,那么为什么要创建它?!
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'prodCatRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class java.lang.Object
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String categoryName;
@ManyToMany(mappedBy = "categories")
private Set<Product> products;
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Category other = (Category) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
public interface ProdCatRepository<T> extends CrudRepository<T, Long> { }
public interface ProductRepository extends CrudRepository<Product, Long> { }
public interface CategoryRepository extends CrudRepository<Category, Long> { }