请帮助了解如何为该MySQL查询编写JPA查询
select car_xid from t_booking where from_date between '06/23/2019' and
'06/24/2019'
or to_date between '06/23/2019' and '06/24/2019' or from_date <
'06/23/2019' or to_date > '06/24/2019'
在MySQL中,此查询返回car_xid为1;
我有Booking Bean,它映射到MySQL DB中的t_booking表 和Cars表,该表已映射到MySQL DB中的t_cars
车豆:
@Entity
@Table(name="m_cars")
public class Cars {
public Cars() {
}
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="car_pid")
private int carPid;
@Column(name="car_number")
private String carNumer;
@Column(name="car_name")
private String carName;
@Column(name="car_type")
private String carType;
@Column(name="passenger_number")
private int paxNumbers;
@Column(name="available_from")
private Date availableFrom;
@Column(name="available_to")
private Date availableTo;
@Column(name="rates")
private float ratesPeHour;
@Column(name="deposite")
private float deposite;
@Column(name="is_active")
private char isActive;
@Column(name="from_city")
private String fromCity;
@Column(name="to_city")
private String toCity;
@Column(name="img_url")
private String imgUrl;
}
Booking Bean:
@Entity
@Table(name="t_booking")
public class Booking {
public Booking() {
}
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
@Column(name="booking_pid")
private int bookingPid;
@OneToOne
@JoinColumn(name="user_xid")
private User user;
@Column(name="from_city")
private String fromCity;
@Column(name="to_City")
private String toCity;
@Column(name="from_date")
private Date fromDate;
@Column(name="to_date")
private Date toDate;
@Column(name="car_number")
private String carNumber;
@Column(name="deposite")
private float deposite;
@Column(name="passenger_number")
private int paxNumber;
@Column(name="from_loc")
private String fromLoc;
@Column(name="to_loc")
private String toLoc;
@OneToOne
@JoinColumn(name="car_xid")
private Cars cars;
@Column(name="transaction_id")
private String transactionId;
@Column(name="total_amount")
private float totalAmount;
}
我已经尝试过JPA查询
public interface BookingRepo extends JpaRepository<Booking, Integer>{
@Query("select b.cars.carPid from Booking b,Cars car where b.fromDate
between ?1 and ?2 or b.toDate between ?3 and ?4 or b.fromDate < ?5 and
b.toDate > ?6")
List<Integer> getBookedCarId(Date fromDate,Date toDate,Date from,Date
to,Date frmDate,Date tDate);
}
此返回null,但实际上应该返回1。
Actual result = JPA Query返回空值。
expected result =应该返回car_xid为1
答案 0 :(得分:1)
尝试这个。如您所见,在where子句中不需要Cars:为Booking Table创建对象
@Query("select b.cars.carPid from Booking b where b.fromDate between ?1 and ?2 or b.toDate between ?3 and ?4 or b.fromDate < ?5 and b.toDate > ?6")
List<Integer> getBookedCarId(Date fromDate, Date toDate, Date from, Date to, Date frmDate, Date tDate);