Vladimir Jotov
Vladimir Jotov

Reputation: 188

How to avoid "code smell" when using @NamedEntityGraphs annotation?

In IntellyJ Idea I have installed SonarLint.

Code analizer says that I have to remove the "NamedEntityGraphs" wrapper from this annotation group. Here is my code:

@NamedEntityGraphs({
        @NamedEntityGraph(
                name = Application.ONLY_NAME,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar")
                }
        ),
        @NamedEntityGraph(name = Application.FULL,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar"),
                        @NamedAttributeNode("buzz")
                }
        )
})
public class SomeClass {  ...  }

Here are the arguments from SonarLint:

Annotation repetitions should not be wrapped Code smell, Minor, java:S1710

Before Java 8 if you needed to use multiple instances of the same annotation, they had to be wrapped in a container annotation. With Java 8, that's no longer necessary, allowing for cleaner, more readable code. Note that this rule is automatically disabled when the project's sonar.java.source is lower than 8.

Noncompliant Code Example

@SomeAnnotations({ // Noncompliant
        @SomeAnnotation(..a..)
        @SomeAnnotation(..b..)
        @SomeAnnotation(..c..)
})
public class SomeClass {  ...  }

 

Compliant Solution

@SomeAnnotation(..a..)
@SomeAnnotation(..b..)
@SomeAnnotation(..c..)
public class SomeClass {  ...  }

QUESTION:

Does enybody know how to organize @NamedEntityGraphs in order to get complient code?

Upvotes: 1

Views: 3173

Answers (2)

SSK
SSK

Reputation: 3766

You have given your answer in your code itself. Just remove @NamedEntityGraphs

@NamedEntityGraph(name = Application.ONLY_NAME,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar")
                }
        )
@NamedEntityGraph(name = Application.FULL,
                attributeNodes = {
                        @NamedAttributeNode("foo"),
                        @NamedAttributeNode("bar"),
                        @NamedAttributeNode("buzz")
                }
        )
public class SomeClass {  ...  }

Upvotes: 0

Jens Schauder
Jens Schauder

Reputation: 81930

Just do as the tool told you (and also @Slaw in the comments): Remove the outer annotation and just repeat @NamedEntityGraph which is ok since it is @Repeatable with JPA 2.2

@NamedEntityGraph(
        name = Application.ONLY_NAME,
        attributeNodes = {
                @NamedAttributeNode("foo"),
                @NamedAttributeNode("bar")
        }
),
@NamedEntityGraph(name = Application.FULL,
        attributeNodes = {
                 @NamedAttributeNode("foo"),
                 @NamedAttributeNode("bar"),
                 @NamedAttributeNode("buzz")
       }
)
public class SomeClass {  ...  }

Upvotes: 2

Related Questions