我想使用 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 可能已经在他的评论中发布了最佳答案。
关于java - 使用 JPA 方法比较两个类属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56414725/