Criteria API createAlias 在包含空值的列上终止查询

Criteria API createAlias on column containing null values kills the query

我确实有一个名为 "Address" 的简单实体,它有几个属性和关系,它们是自己定义的,有些是从某个超类继承的。

public class Base {
  private UUID id;
  private Date createdAt;
  @NotNull
  private User createdBy;  // User is a related Entity that cannot be null
  private DataImport dataImport;  // DataImport is another related entity that can be null
  //getter & setter
}

public class Address extends Base {
  private String street;
  @NotNull
  private Country country;  // related entity that can't be null
  //getter & setter
}

我想要实现的是通过一个使用条件 API 的查询,我想获得包含所有简单属性(如街道和 createdAt)的地址对象列表。同时,如果存在,我只想要相关实体的 ID:createdBy.id、dataImport.id 和 country.id。

我几乎可以使用以下条件查询:

entityManager.getDelegate().createCriteria(Address.class).add(criteriaExample)
 .excludeZeroes()
 .createAlias("createdBy", "subcreatedBy")
 .createAlias("country", "subcountry")
 .setProjection(Projections.projectionList().add(Projections.property("id").as("id"))
    .add(Projections.property("createdAt").as("createdAt"))
    .add(Projections.property("street").as("street")).
    .add(Projections.property("subcreatedBy.id").as("createdBy.id"))
    .add(Projections.property("subcountry.id").as("country.id")))
  .setResultTransformer(new AliasToBeanNestedResultTransformer(Address.class));

List<Address> result = criteria.list();

这太完美了!

当我只为 dataImport 关系添加 "alias" 时出现问题。

...createAlias("dataImport", "subdataImport")...

即使不将 dataImport.id 的投影添加到查询中,它 returns 也是一个空列表,意思是 list.size() = 0,只要我添加这个别名。

我目前的猜测是,我不能为可为 null 的 属性 设置别名。有没有人知道解决方案可能是什么?所以,当相关实体不为空时,我想获取它的 ID。当未设置关系时,我希望它只是空值。

提前致谢。

愚蠢的我应该阅读文档并设置CriteriaSpecification.LEFT_JOIN。

...createAlias("dataImport", "subdataImport", CriteriaSpecification.LEFT_JOIN)...