JPA - 选择ManyToMany字段

时间:2015-10-16 17:49:32

标签: java 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:无法准备语句

如何撰写正确的查询?

1 个答案:

答案 0 :(得分:1)

尝试此查询:

SELECT DISTINCT r FROM Seat s JOIN s.availableRates r WHERE s.performance = :performance