使用 JPQL 通过连接表进行查询时出错
Posted
技术标签:
【中文标题】使用 JPQL 通过连接表进行查询时出错【英文标题】:Error while using JPQL to query by joining tables 【发布时间】:2020-10-05 03:33:25 【问题描述】:我正在尝试使用彼此具有一对多关系的路线实体的源和目的地属性来获取火车实体列表。以路线 ID 作为外键的火车表。表名分别是路线和火车。请帮助我,因为查询正在抛出java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.infyrail.app.repository.RouteRepository.findBySourceDestination(java.lang.String,java.lang.String)!
RouteRepository:
public interface RouteRepository extends JpaRepository<RouteEntity, Integer>
@Query("SELECT t FROM train t JOIN route r WHERE r.source=?1 AND r.destination=?2")
public List<TrainEntity> findBySourceDestination(String source,String destination);
路由实体:
@Entity
@Table(name="route")
public class RouteEntity
@Id
@GenericGenerator(name="route_id",
strategy="com.infyrail.app.generator.RouteIdGenerator")
@GeneratedValue(generator = "route_id")
@Min(value = 100)
@Max(value = 999)
Integer id;
String source;
String destination;
@OneToMany(mappedBy = "route",
cascade=CascadeType.ALL,orphanRemoval = true)
private List<TrainEntity> trainList;
public Integer getId()
return id;
public void setId(Integer id)
this.id = id;
public String getSource()
return source;
public void setSource(String source)
this.source = source;
public String getDestination()
return destination;
public void setDestination(String destination)
this.destination = destination;
public List<TrainEntity> getTrainList()
return trainList;
public void setTrainList(List<TrainEntity> trainList)
this.trainList = trainList;
火车实体:
@Entity
@Table(name="train")
public class TrainEntity
//@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@GenericGenerator(name="train_id",
strategy="com.infyrail.app.generator.TrainIdGenerator")
@GeneratedValue(generator = "train_id")
@Min(value = 100)
@Max(value = 999)
Integer id;
String trainName;
String arrivalTime;
String departureTime;
Double fare;
@ManyToOne(fetch = FetchType.LAZY)
@Autowired
RouteEntity route;
public RouteEntity getRoute()
return route;
public void setRoute(RouteEntity route)
this.route = route;
public Integer getId()
return id;
public void setId(Integer id)
this.id = id;
public String getTrainName()
return trainName;
public void setTrainName(String trainName)
this.trainName = trainName;
public String getArrivalTime()
return arrivalTime;
public void setArrivalTime(String arrivalTime)
this.arrivalTime = arrivalTime;
public String getDepartureTime()
return departureTime;
public void setDepartureTime(String departureTime)
this.departureTime = departureTime;
public Double getFare()
return fare;
public void setFare(Double fare)
this.fare = fare;
【问题讨论】:
只使用findByRouteSourceAndRouteDestination
,不要写查询。
【参考方案1】:
这就是问题所在。您的查询应该定义表 bean 名称而不是实际的表名称。
在您的情况下,您应该使用 TrainEntity 而不是 train 和 RouteEntity 而不是 route。
public interface RouteRepository extends JpaRepository<RouteEntity, Integer>
@Query("SELECT t FROM TrainEntity t JOIN RouteEntity r WHERE r.source=?1 AND r.destination=?2")
public List<TrainEntity> findBySourceDestination(String source,String destination);
【讨论】:
以上是关于使用 JPQL 通过连接表进行查询时出错的主要内容,如果未能解决你的问题,请参考以下文章