添加OR语句时,HQL抛出异常

时间:2015-05-08 08:26:48

标签: java hibernate orm hql

我有以下HQL:

select r from Route r where r.startingStop.description = 'Railway Station'

现在,我已将我的HQL修改为:

select r from Route r where r.startingStop.description = 'Railway Station' or r.destinationStop.description = 'Railway Station'

我得到以下例外:

org.hibernate.exception.DataException: Subquery returns more than 1 row

我已经在Stackoverflow上研究了这个异常,但我仍然无法解决我的问题。提前谢谢。

我的字段(单向映射):

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "starting_stop")
private BusStop startingStop;

@ManyToOne(fetch=FetchType.EAGER)
@JoinColumn(name = "destination_stop")
private BusStop destinationStop;

2 个答案:

答案 0 :(得分:0)

只要我们无法知道您想要的选择或输出,这里有一些提示,可以指导您解决问题。

原因:外部查询必须使用其中一个关键字ANYALLINNOT IN来指定要比较的值因为子查询返回了多行

操作:使用ANYALLINNOT IN指定要比较或重新命名查询的值,行被检索。

建议:如果不要关心列表中的值,或者确定它们是,那么请尝试将rownum=1添加到子查询条件中相同。

答案 1 :(得分:0)

查询工作正常,但问题是它返回的行数超过1行。您尝试将结果用作1个对象,但使用子句时,它会返回多个并抛出异常。 例如,如果您将它用作子查询,并说:

Something =(您的查询)它将失败,因为您的查询返回2个结果。

如果您尝试将其映射到单个对象,则也会出现问题,因为您获得的结果超过1个。