Skip to content

Commit fff49da

Browse files
committed
PMD: Use a Set instead of a List in entity relations
Fixes xdev-software/java-setup-template#5
1 parent 628f471 commit fff49da

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

.config/pmd/java/ruleset.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,30 @@ If you are 100% sure that you escaped the value properly and you have no better
394394
</properties>
395395
</rule>
396396

397+
<!-- Jakarta Persistence -->
398+
<rule name="AvoidListAsEntityRelation"
399+
language="java"
400+
message="Use a Set instead of a List in entity relations"
401+
class="net.sourceforge.pmd.lang.rule.xpath.XPathRule"
402+
externalInfoUrl="https://www.baeldung.com/spring-jpa-onetomany-list-vs-set#bd-pros-and-cons">
403+
<description>
404+
`List` allows duplicates while a `Set` does not.
405+
A `Set` also prevents duplicates when the ORM reads multiple identical rows from the database (e.g. when using JOIN).
406+
</description>
407+
<priority>2</priority>
408+
<properties>
409+
<property name="xpath">
410+
<value>
411+
<![CDATA[
412+
//ClassDeclaration[pmd-java:hasAnnotation('jakarta.persistence.Entity')]
413+
//FieldDeclaration[pmd-java:hasAnnotation('jakarta.persistence.ManyToMany') or pmd-java:hasAnnotation('jakarta.persistence.OneToMany')]
414+
/ClassType[pmd-java:typeIs('java.util.List')]
415+
]]>
416+
</value>
417+
</property>
418+
</properties>
419+
</rule>
420+
397421

398422
<!-- Rules from JPinPoint with slight modifications -->
399423
<!-- https://github.com/jborgers/PMD-jPinpoint-rules -->

0 commit comments

Comments
 (0)