1

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?

Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
Vladimir Jotov
  • 188
  • 1
  • 8
  • 4
    Why not just remove `@NamedEntityGraphs` since, as noted by your tool, it's unnecessary? The `@NamedEntityGraph` annotation is `@Repeatable`. – Slaw Jan 20 '21 at 05:30

2 Answers2

2

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 {  ...  }
Jens Schauder
  • 77,657
  • 34
  • 181
  • 348
0

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 {  ...  }
SSK
  • 3,444
  • 6
  • 32
  • 59