JPA - 从连接表中检索信息

时间:2016-05-23 17:10:13

标签: java sql jpa jpql

所以我想说我有这两个类:

@Entity
public class Reservation extends Observable implements Serializable {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;

private static final long serialVersionUID = 1L;

@ManyToOne(cascade = CascadeType.MERGE)
private ReservationState state;

@ManyToOne(cascade = CascadeType.MERGE)
private CafeteriaUser user;

@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="idMeal",referencedColumnName= "idMeal")
private Meal meal;

@Entity
public class Meal implements Serializable {

private Price price;
private MealType mealType;
private Dish m_dish;

@Temporal(javax.persistence.TemporalType.DATE)
private Calendar date;

@ManyToOne (cascade = CascadeType.ALL)
private Menu menu;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long idMeal;

public Meal() {
}

现在,我遇到了麻烦,无法连接'这两张桌子。什么相当于在给定日期之前获得预订(从预约r中选择*,在膳食中选择r.idMeal = m.IDMEAL和m。" DATE" =' 2016-01- 01')在JPQL中?

似乎使用r.idMeal = m.idMeal给了我"收集价值的路径' r.idMeal'无法解析为有效的关联字段。"错误

1 个答案:

答案 0 :(得分:1)

基本的JPQL就像这样

SELECT r FROM Reservation r JOIN r.meal m WHERE m.date = :date

并将日期作为参数传递。从绝大多数JPQL教程中可以看出这一点