java - 我的应用程序是多对一不工作的问题
问题描述
我在我的 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> {
}
解决方案
也许你的吸气剂getId
不是getItemID
看到Spring Data JPA Unable to locate Attribute with the given name
无论如何,最好尽可能多地发布代码
推荐阅读
- amazon-route53 - Route 53 管理的 DNS 中的问题
- swiftui - SwiftUI 禁用列表边框 iOS 14
- html - 砌体框布局无法根据使用 CSS 的文本工作
- linux - centos 的自定义命令
- python - 卷积神经网络中的输入和输出形状
- bash - 为什么有些脚本与 #! /bin/sh 和一些带有 #! /bin/ksh 在 Solaris 机器上?
- git - 如何解释 git reset --soft && git checkout 的行为
- excel - Access/Excel/VBA 中的日期到 Weeknum
- flutter - FirebaseDynamicLinks getInitialLink() 多次触发
- google-apps-script - GmailApp.getInboxThreads() 跳过电子邮件