首页 > 解决方案 > 我的应用程序是多对一不工作的问题

问题描述

我在我的 Web 应用程序中使用了 Spring DATA JPA。我的目标是使用来自 MySQL 的两个表的 API REST crud 方法。项目是目标,然后数据库有 2 个表:Item 和 AutoBrand,它们是关联关系。一个项目可以获得比 1 个更多的汽车品牌。但我的应用程序遇到问题,即从项目表中找不到汽车品牌表的 id。

例如: 获取 localhost:8081/autoeshop/items - 200 OK 获取 localhost:8081/autoeshop/item/1/brands - 500 内部服务器错误

系统报告消息:无法在此 ManagedType [com.autoparts.autoeshop.model.Item] 上找到具有给定名称 [id] 的属性;嵌套异常是 java.lang.IllegalArgumentException: Unable to locate Attribute with the given name [id] on this ManagedType [com.autoparts.autoeshop.model.Item]

我的代码:Item.java

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long itemID;

    private String name;
    private String provider;
    private double price;
    private int quantity;

    @OneToMany( cascade = CascadeType.ALL, mappedBy = "item")
    private Set<AutoBrand> brands;

AutoBrand.java

    @Id
    @GeneratedValue(strategy=GenerationType.AUTO)
    private Long brandID;   
    private String name;
    private String model;
    private int year;

    @ManyToOne
    @JoinColumn(name="itemid", nullable = false)
    private Item item;

项目rep.java

public interface ItemRepository extends JpaRepository<Item, Long> {

}

AutoBrand Rep.java

public interface AutoBrandRepository extends JpaRepository<AutoBrand, Long> {

}

标签: javarestapijpacrud

解决方案


也许你的吸气剂getId不是getItemID 看到Spring Data JPA Unable to locate Attribute with the given name

无论如何,最好尽可能多地发布代码


推荐阅读