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

Time:02-11

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?

CodePudding user response:

The method naming for "attributes" is based on the java beans convention i.e. 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();
}
  • Related