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

JPA Entity check makes incorrect assumption about getter method names. #829

Closed
sithmein opened this issue Jun 5, 2023 · 4 comments
Closed
Labels

Comments

@sithmein
Copy link

sithmein commented Jun 5, 2023

I like the new JPA entity check for using getters in the equal method instead of the instance variables directly. However, it makes assumptions about the naming of members methods that are not always correct.
We have a coding convention where instance variables are prefixed with m_, e.g. m_groups. The getter method is then derived as getM_groups which is wrong (it's getGroups as one would expect), see

.
In the end this leads to the incorrect error message -> JPA Entity: direct reference to field m_groups used in equals instead of getter getM_groups. Which, when looking at the equals-implementation is just wrong.

In order to fix this issue I suggest adding a configuration option where you can specify an optional prefix for instance variables which is then removed from the name when deriving the getter method, e.g.

EqualsVerifier.forClass(Foo.class).withMemberVariablePrefix("m_")...
@jqno
Copy link
Owner

jqno commented Jun 7, 2023

I see your point. I'm not a big fan of these kinds of conventions, but sometimes we don't get the choice...

I'll have to think a bit about the best way to support this. Maybe it isn't always a prefix, would it be better to provide a function instead of a prefix?

I'll let you know when I come up with something. If you have more thoughts, they're certainly welcome.

@jqno jqno added the accepted label Jun 7, 2023
@sithmein
Copy link
Author

sithmein commented Jun 9, 2023

You mean a function that the user provides which derives the method name from the member variable name? That would also work and even be more flexible.

@jqno
Copy link
Owner

jqno commented Jul 8, 2023

I've just released version 3.15. It has a withFieldnameToGetterConverter method.

This should solve your situation:

EqualsVerifier
    .forClass(Foo.class)
    .withFieldnameToGetterConverter(
        fn -> "get" + Character.toUpperCase(fn.charAt(2)) + fn.substring(3)
    )
    .verify();

@jqno jqno closed this as completed Jul 8, 2023
@sithmein
Copy link
Author

Thanks a lot! I will give it a try soon.

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

No branches or pull requests

2 participants