spring-data-jpa 如何使用jpql查询连接多个表

rur96b6h  于 2022-11-10  发布在  Spring
关注(0)|答案(1)|浏览(187)

我正在使用flight_management_system,其中我有三个实体UserEntityFlightEntityBookEntityUserEnityBookEntity关联,FlightEntityBookEntity关联。当我试图运行我的项目时,我在eclipse ide中遇到错误。
错误

Caused by: org.hibernate.QueryException: could not resolve property: flightName of: com.flightbook.entity.FlightEntity

下面是我的代码:

实体

public class UserEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String userName;
    private String password;

    @JsonBackReference("auth")
    @ManyToOne(targetEntity = AuthGroupEntity.class)
    @JoinColumn(name = "auth_id")
    private AuthGroupEntity authGroupEntity;

    @JsonManagedReference("booking")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "userEntity")
    private List<BookEntity> bookEntities;

}

public class FlightEntity {
    @Id
    private String flightId;

    private String flightname;
    private String from;
    private String to;

    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date scheduleDate;

    @JsonManagedReference("flight")
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "flightEntity")
    private List<BookEntity> bookEntities;

}

public class BookEntity {
    @Id
    private String bookingId;

    private String pasengerName;
    private String status;

    @JsonBackReference("flight")
    @ManyToOne(targetEntity = FlightEntity.class)
    @JoinColumn(name = "flight_id")
    private FlightEntity flightEntity;

    @JsonBackReference("booking")
    @ManyToOne(targetEntity = UserEntity.class)
    @JoinColumn(name = "user_id")
    private UserEntity userEntity;

}

型号

public class BookingList {
    private String bookingId;
    private String pasengerName;
    private String status;
    private Date scheduleDate;
    private String flightName;

    public BookingList(String bookingId, String pasengerName, String status, Date scheduleDate, String flightName) {
        this.bookingId = bookingId;
        this.pasengerName = pasengerName;
        this.status = status;
        this.scheduleDate = scheduleDate;
        this.flightName = flightName;
    }
    ....
    ....
}

存储库

@Query("SELECT new com.flightbook.model.BookingList(b.bookingId, b.pasengerName, b.status, f.scheduleDate, f.flightName) FROM BookEntity b JOIN b.flightEntity f JOIN b.userEntity u WHERE u.id = ?1")
    List<BookingList> getFlightBookingByUserId(Long user_id);
eimct9ow

eimct9ow1#

您的属性是flightname,而不是FlightEntity的flightName

相关问题