- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 Java EE 6 并使用 JPA 的 javax.persistence.Entitymanager 查询数据库。我有一个 JPQL 查询代码片段,看起来像这样:
Query query = entityManager.createQuery("
select A.propertyX, B.propertyY, C.propertyZ
from TableA A, TableB B, TableC C
where A.id = :id and B.id = A.id and C.type = B.type
");
query.setParameter("id", id);
Object[] result = (Object[]) query.getSingleResult();
其中 propertyX/Y/X 都是对其他实体的引用。在我的例子中,来自 TableA、TableB 和 TableC 的匹配行都存在。对于匹配的行,TableA.propertyX 和 TableB.propertyY 保存值,而 TableC.propertyZ 为 null(并且不是必需的)。
我希望它执行并返回一个 Object[] 数组,其中前两个元素(propertyX 和 propertyY)的值为 null,第三个元素(propertyZ)为 null。
但是,当 propertyZ 为 null 时,将抛出 NoResultException。如果我更改数据以使 propertyZ 不为空,则查询将执行并返回一个值。
明显的解决方法是选择整个根实体而不是任何子属性,例如'C' 而不是 'C.propertyZ',然后从实体对象中获取属性。但是,我希望它在不这样做的情况下按我预期的那样工作。
最佳答案
如果对于 A 和 B 中的给定行,C 中有一行 C.type = B.type,但该行的 propertyZ 列为空,那么您的查询应该返回一条记录是对的.
但是,如果对于 A 和 B 中的给定行,在 C 中没有匹配的行,其中 C.type = B.type,那么您的查询将不会返回任何结果。这与JPQL无关,与SQL无关
如果你希望后一种情况仍然返回一条在propertyZ字段中为null的记录,你需要使用OUTER JOINs
HTH
关于java - JPA 和 JPQL : NoResultException when selecting from multiple tables where one value is null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8000247/
我想写这样的查询:- Select u from bookmaster u order by Year(b.createDate),Month(b.createDate),day(b.createDa
例如:选择不同的新 JPQA.QueryResultObjectV1(space.name,spaceID) "+ "from Space space,Building building,Floor
例如:选择不同的新 JPQA.QueryResultObjectV1(space.name,spaceID) "+ "from Space space,Building building,Floor
我有两张 table 第一张表 - 事件第二张表 - 类别 @Entity public class Event { @Id @GeneratedValue(strategy = Generation
我在我的 DAO 中使用 entityManager。 要创建表格,我使用: String CREATE_TABLE = "CREATE TABLE if not exists cats (name
在 JPQL 中,我可以通过以下方式检索实体: query = entityManager.createQuery("select c from Category c"); List categori
我需要执行以下选择: select c.address from Customer c where lower(trim(c.name)) = :name 但是我得到以下异常: javax.persi
我无法使此查询正常工作: Query query = eManager.createQuery("select c FROM News c WHERE c.NEWSID = :id",News.cla
我在我的 msql 表列中有 Long 类型,该列表示位标记。 我需要选择为其设置了所有几个标志的对象: columnValue & flags = columnValue 我尝试: javax.pe
请查看这篇文章的末尾(找到了另一种但也不起作用的方法)。第二种方式应该是更好的方式,如果有一天它起作用的话;) 我有一个由两个多对多关系组成的实体:MainEntity 有很多 A,MainEntit
首先声明我不是数据库专家。这些东西把我搞糊涂了,但不幸的是我在工作中被迫这样做,所以我有点卡住了:-) 我试图通过在 JPQL 中工作来获得一个复杂的订单,但我一点运气都没有。 我当前的查询如下所示:
我需要将此自联接转换为 JPQL: SELECT s1.* FROM site AS s1 JOIN (SELECT site_type, MAX(last_update_date) AS LastU
如果我有一个扩展 Fruit 的类 Apple,我如何编写一个 JPQL 查询来返回所有严格意义上是 Fruits 而不是 Apples 的对象? 最佳答案 这在 JPA 2.0 中是可能的,使用 T
我正在尝试获取 Order[] 数组,其中包括未收到相关文档的所有订单。 我尝试了这个查询,它返回了正确的行数。 @Query("Select o FROM Order o INNER JOIN o
我正在尝试根据用户可以选择的可能日期列表对日期进行搜索。使用日历,我要求用户输入一个日期,然后我想获取在“data_possibile”列表中具有该日期的所有可能数据包。 这是我正在使用的查询: @N
我想构建一个 JPQL 查询来将该结构的数据映射到这个 DTO: @AllArgsConstructor class UserDTO { long userId; long countOfCo
我必须对表执行批量更新。 做一个快速的例子: UPDATE Book b SET b.amount = b.amount + 1 WHERE b IN ( :books ) 问题是 b.amount
我有以下 JPA实体: @Entity class UserClient{ @Id @GeneratedValue(strategy = GenerationType.AUTO)
我有一个实体(例如 Setting),其中包含集合(例如 Settings.languages)和另一个对象(Language 的实例)。 有没有办法选择其集合实体中包含所需属性值的所有实体?例如获取
我正在使用 spring data jpa:过滤父对象的子对象的最佳方法是什么?在我下面的例子中我想要具有事件子对象的父对象,也只想要事件子对象作为父对象的列表 @Query(select dis
我是一名优秀的程序员,十分优秀!