【发布时间】:2012-12-08 22:45:06
【问题描述】:
我的猜测是这个问题在某个地方得到了回答,但我似乎找不到合适的关键词来搜索。
我有这些实体:
@Entity
Entity1{
<random fields>
@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "entity_2")
Entity2 e2;//might be null.
}
@Entity
Entity2{
<random fields>
String name;
}
我想编写一个查询,返回我的所有 Entity1 的名称,它们按 Entity2 的名称排序。我试过这个:
SELECT e1 from Entity1 e1 ORDER BY e1.e2.name ASC
上述查询(在mysql上测试)的问题是它只返回Entity1,其中e2不为null。
我需要的是一种获取所有 Entity1 的方法,即使 e2 为空。
附言。我也试过这些,结果相同:
SELECT e1 from Entity1 e1 ORDER BY e1.e2 ASC, e1.e2.name ASC
SELECT e1 from Entity1 e1 ORDER BY e1.e2.name ASC, e1.e2 ASC
提前致谢!
//hql新手
编辑:
添加了我使用的 jpa -annotations。
【问题讨论】: