Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to ignore a certain Getter in EntityView? #1901

Open
ilxqx opened this issue May 6, 2024 · 1 comment
Open

How to ignore a certain Getter in EntityView? #1901

ilxqx opened this issue May 6, 2024 · 1 comment

Comments

@ilxqx
Copy link

ilxqx commented May 6, 2024

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
Copy link
Member

beikov commented May 6, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants