Blazebit / blaze-persistence

Rich Criteria API for JPA providers
https://persistence.blazebit.com
Apache License 2.0
687 stars 85 forks source link

How to ignore a certain `Getter` in EntityView? #1901

Open ilxqx opened 1 week ago

ilxqx commented 1 week ago

First of all, thank you very much to the authors of the library for their great work. This project is truly remarkable!

The scenario is as follows: In an EntityView, besides the Getters for fields retrieved from the database, there might also be defined translation fields used to store values that are complexly transformed and translated based on certain fields in a view.

Here is a rough scenario:

@EntityView(Person.class)
    interface PersonView {

        @IdMapping
        String getId();

        @Mapping("name")
        String getName();

        @Mapping("status")
        String getStatus();

        // This is my assumed annotation,
        // used to ignore this property as part of the SQL select statement.
        @MappingIgnore
        // @Mapping("null")
        // This is a solution, but it will result in one more 'null' in the select,
        // which is not elegant and often causes confusion and misunderstanding.
        // It seems like @MappingParameter("") has the same issue.
        String getStatusName();

        String setStatusName(String statusName);

        @PostLoad
        default void init() {
            setStatusName(
                // Here it is assumed that a very complex transformation process has been experienced.
                getStatus().equals("A") ? "Active" : "Inactive"
            );
        }
    }

Although it seems that using the constructor of an abstract class can solve this problem, I find that abstract classes have too many limitations and are extremely cumbersome to use. Interfaces are more convenient and also support multiple inheritances.

It would be great if we could add a @MappingIgnore annotation to ignore specific getters. I wonder if the maintainers of the library have any thoughts on adding this feature?

beikov commented 1 week ago

Hi and thanks for reporting your experience. Indeed, a @MappingIgnore annotation sounds like a useful thing to have. So the entity view implementation would still create a mutable field for this, but just ignore it for the purpose of forming the query.