如何使用 Blaze Persistence 实体视图映射布尔字段表达式

How to map a boolean field expression using Blaze Persistence Entity View

我有以下实体视图模型,我正在尝试将其从 spring 数据 @Projection 转换为等效的 blaze @EntityView

@Projection(types = Car.class)
@EntityView(Car.class)
public interface CarEntityView {

    String getMake();

    String getModel();

    Owner getOwner();

    @Mapping("owner.id")
    @Value("#{target.owner?.id}")
    UUID getOwnerId();

    @Mapping("owner <> null")
    @Value("#{target.owner != null}")
    boolean hasOwner();
}

下面布尔表达式的 spring 注释工作正常

@Value("#{target.owner != null}")

但我无法弄清楚似乎不起作用的等效 blaze 实体视图映射的语法:

@Mapping("owner <> null")

像这样映射布尔表达式的正确方法是什么?

“属性”的方法命名基于 java beans 约定,即如果您想要 boolean return,该方法必须命名为 isOwner(),但是 Blaze-Persistence Entity-Views 也允许您使用 getOwner()。我想在你的情况下,像 isOwned 这样的名字可能最合适。

另一件需要考虑的事情是映射表达式只能产生标量表达式,所以你还不能放入谓词。如果您想跟踪它,有一个开放的功能请求:https://github.com/Blazebit/blaze-persistence/issues/340

同时,你必须把它包装成一个 case when 表达式,像这样:

@Projection(types = Car.class)
@EntityView(Car.class)
public interface CarEntityView {

    String getMake();

    String getModel();

    Owner getOwner();

    @Mapping("owner.id")
    @Value("#{target.owner?.id}")
    UUID getOwnerId();

    @Mapping("case when owner is not null then true else false end")
    @Value("#{target.owner != null}")
    boolean hasOwner();
}