java - 使用 JPA 方法比较两个类属性
问题描述
我想使用 JPA 方法约定比较两个属性。
这是我的课
@Entity
@Table(name = "aircrafts")
public class Aircrafts {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Size(max = 45)
@Column(name = "number", length = 45)
private String number;
@Column(name = "capacity")
private int capacity;
@Column(name = "seats_taken")
private int seatsTaken;
}
这是我要实现的方法:
public interface AircraftsRepository extends JpaRepository<Aircrafts, Long> {
public List<Aircrafts> findBySeatsTakenLessThanCapacity();
}
但是我得到了这个例外:
PropertyReferenceException: No property lessThanCapacity found for type int! Traversed path: Aircrafts.seatsTaken.
我试过使用 int 和 Integer 但我得到了同样的例外。哪个是正确的方法名称?
解决方案
我认为@benji2505,正确地提到数据模型混合了“应该”在不同表中的东西。通常人们会期待两张表:飞机,航班。
然后你可以很容易地使用:
List<Flight> flightsWithFreeSeats = aircraftRepository
.findAll()
.stream()
.map(aircraft ->
flightRepository.findByAircraftAndSeatsTakenLessThen(aircraft, aircraft.getCapacity)
)
.collect(Collectors.toList)
对于当前模型,@JZ Nizet 可能已经在他的评论中发布了最佳答案。
推荐阅读
- jenkins - Jenkins 错误无法生成脚本文件
- java - 读取配置文件返回 null
- python - 根据列定位从两个数据框中获取数字的百分比变化
- javascript - 多个图像(xml -> javascript -> CSS)被覆盖,仅显示 1 个图像
- django - 序列化器 - Django REST 框架 - 序列化器字段可能命名不正确,并且与“str”实例上的任何属性或键都不匹配
- twitter-bootstrap-3 - 控制对齐 input-group-btn
- android - Android 无法打开文件,FileNotFoundException(Permission denied),但是 PermissionRead 被授予
- node.js - 需要使用节点 js 将 JSON 从端点发布到 Prometheus
- shell - 在 HDFS 中查找并删除零字节文件
- datatable - 使用ajax在数据表中进行简单分页而不从服务器发送总数