简体   繁体   中英

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

I have the following entity view model which I'm trying to convert from a spring data @Projection to the blaze @EntityView equivalent

@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();
}

The spring annotation for the boolean expression below works fine

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

But I can't figure out the syntax for the equivalent blaze entity view mapping which doesn't seem to work:

@Mapping("owner <> null")

What is the correct way to map a boolean expression like this?

The method naming for "attributes" is based on the java beans convention ie if you want a boolean return, the method must be named isOwner() , but Blaze-Persistence Entity-Views also allows you to use getOwner() . I guess in your case, a name like isOwned might fit best.

Another thing to consider is that mapping expressions can only result in scalar expressions, so you can't put in a predicate yet. There is an open feature request for this though in case you want to track it: https://github.com/Blazebit/blaze-persistence/issues/340

In the meantime, you have to wrap it into a case when expression, like this:

@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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM