我正在使用Spring jpa从两个表中获取数据,但是当我通过查询获取数据时,会发生错误。
两个表:“跳闸”和“跳闸状态”
我的模特: 我的实体:
@Entity
@Table(name="trip")
public class Trip {
@Id
@GeneratedValue(strategy=GenerationType.AUTO)
@Column(name="id")
private int id;
@Column(name="start_address")
private String start_address;
@Column(name="end_address")
private String end_address;
@Column(name="pickup_latitude")
private float pickup_latitude;
@Column(name="pickup_longitude")
private float pickup_longitude;
@Column(name="fee")
private float fee;
@Column(name="canceled_by")
private String canceled_by;
@Column(name="canceled_at")
private Instant canceled_at;
@Column(name="actual_distance")
private float actual_distance;
@Column(name="actual_price")
private float actual_price;
/////////////////////////////////////////////
@Column(name="estimated_distance")
private float estimated_distance;
@Column(name="estimated_time")
private float estimated_time;
@Column(name="estimated_price")
private float estimated_price;
@Column(name="actual_time")
private float actual_time;
@ManyToOne(optional = false)
@NotNull
private TripStatus tripStatus;
// getter and setter
我的资料库:
@Repository
public interface TripRepository extends JpaRepository<Trip, Integer> {
// TODO Auto-generated method stub
@Query("SELECT trip FROM Trip trip INNER JOIN trip_status ON trip.trip_status_id = trip_status.id WHERE trip_status.id =: id")
public List<Trip> findById(@Param("id") int id);
}
当我运行它时,会发生以下异常的问题:
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: Path expected for join! [SELECT trip FROM com.model.Trip trip INNER JOIN trip_status ON trip.trip_status_id = trip_status.id WHERE trip_status.id =: id]
如何解决该问题?
答案 0 :(得分:0)
JPQL是JPA查询语言。它以面向对象的方式工作,因此您不能将trip_status(列名)与tripStatus(实体属性名)混淆。
首先:您的加入应为INNER JOIN trip.tripStatus ts
第二:在JPQL中,您不必指定trip.trip_status_id = trip_status.id
最后:WHERE ts.id = :id
@Query("SELECT trip FROM Trip trip INNER JOIN trip.tripStatus ts WHERE ts.id =: id")