我使用spring data jpa,我有这些classe(每个都有id...不显示)
public class HOV{
@ManyToOne(optional = false, fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
@JoinColumn(name = "vehicle_type_id")
private VehicleTypes vehicleType;
...
}
public class VehicleTypes{
@OneToMany(mappedBy = "vehicleType")
private List<Vehicles> vehicles = new ArrayList<>();
}
所以在 hov 存储库中,我尝试通过 carsId 进行搜索
List<HOV> findByVehicleTypeVehiclesId(Integer id);
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hOVRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstract java.util.List com.lcm.repository.HOVRepository.findByVehicleTypeVehiclesId(java.lang.Integer)! Illegal attempt to dereference path source [null.vehicleType.vehicles] of basic type
最佳答案
根据您的情况,我认为最好自己提供查询,例如:
@Query("select h from HOV h join h.vehicleType vt left join vt.vehicles v where v.id = ?1")
List<HOV> findWithQuery(Integer vehicleId);
关于spring-data-jpa - 使用 spring 数据搜索嵌套对象,非法尝试取消引用路径源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51616029/