gpt4 book ai didi

java - Spring JPA : Cause path expected for join

转载 作者:行者123 更新时间:2023-11-30 12:05:27 25 4
gpt4 key购买 nike

我正在使用 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]

如何解决这个问题?

最佳答案

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")

关于java - Spring JPA : Cause path expected for join,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56284966/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com