JPA - 选择多对多场
问题描述:
我有以下实体:JPA - 选择多对多场
@Entity
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class Seat extends AbstractEntity<Long> {
@ManyToOne
private Performance performance;
@ManyToMany
private List<Rate> availableRates;
}
我想在JPQL执行以下查询:
SELECT DISTINCT s.availableRates FROM Seat s WHERE :performance = s.performance
,但我一直有如下错误
无法准备声明; SQL [select count(distinct。)as col_0_0_ from seat seat0_,seat_available_rates availabler1_,rate rate2_ where seat0_.identifier = availabler1_.seat_identifier and availabler1_.available_rates_identifier = rate2_.identifier and?= seat0_.performance_identifier];嵌套的异常是org.hibernate.exception.SQLGrammarException:无法准备语句
如何编写正确的查询?
答
尝试此查询:
SELECT DISTINCT r FROM Seat s JOIN s.availableRates r WHERE s.performance = :performance
+0
这就是它!所以我的问题不是JPQL,而是SQL本身:)让我们回到我的书,非常感谢 – Maxime
不能选择多个值字段,按照JPA规范 –