【问题标题】:How to map a boolean field expression using Blaze Persistence Entity View如何使用 Blaze Persistence Entity View 映射布尔字段表达式
【发布时间】:2022-02-02 00:34:36
【问题描述】:

我有以下实体视图模型,我正在尝试将其从 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();
}

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

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

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

@Mapping("owner <> null")

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

【问题讨论】:

    标签: java jpa spring-data-jpa blaze-persistence


    【解决方案1】:

    “属性”的方法命名基于 Java bean 约定,即,如果您想要返回 boolean,则该方法必须命名为 isOwner(),但 Blaze-Persistence Entity-Views 也允许您使用 @987654324 @。我想在你的情况下,像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();
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-26
      • 2013-11-13
      • 2010-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多