Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ruleKey": "S8948",
"hasTruePositives": false,
"falseNegatives": 10,
"falsePositives": 0
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package checks;

import jakarta.persistence.Entity;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.JoinTable;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.OneToMany;
import java.util.List;

public class OneToManyMappingCheckSample {

@Entity
class Author {
@OneToMany // Noncompliant {{Add "mappedBy" or "@JoinColumn" to this "@OneToMany" relationship.}}
// ^^^^^^^^^^
private List<Book> books;
}

@Entity
class Book {
@ManyToOne
private Author author;
}

// Compliant: uses mappedBy
@Entity
class AuthorWithMappedBy {
@OneToMany(mappedBy = "author")
private List<BookWithAuthor> books;
}

@Entity
class BookWithAuthor {
@ManyToOne
@JoinColumn(name = "author_id")
private AuthorWithMappedBy author;
}

// Compliant: uses @JoinColumn on the @OneToMany field
@Entity
class AuthorWithJoinColumn {
@OneToMany
@JoinColumn(name = "author_id")
private List<BookNoRef> books;
}

@Entity
class BookNoRef {
// No reference back to Author
}

@Entity
class AuthorUnidirectional {
@OneToMany // Noncompliant
private List<AnotherBook> books;
}

@Entity
class AnotherBook {
// No reference back
}

// Compliant: uses @JoinTable (explicit join table mapping)
@Entity
class AuthorWithJoinTable {
@OneToMany
@JoinTable(name = "author_books")
private List<BookWithJoinTable> books;
}

@Entity
class BookWithJoinTable {
// No reference back
}

// Property-access: noncompliant getter
@Entity
class AuthorPropertyAccess {
private List<BookPropertyAccess> books;

@OneToMany // Noncompliant
public List<BookPropertyAccess> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccess {
// No reference back
}

// Property-access: compliant with mappedBy
@Entity
class AuthorPropertyAccessMappedBy {
private List<BookPropertyAccessMappedBy> books;

@OneToMany(mappedBy = "author")
public List<BookPropertyAccessMappedBy> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccessMappedBy {
@ManyToOne
private AuthorPropertyAccessMappedBy author;
}

// Property-access: compliant with @JoinColumn
@Entity
class AuthorPropertyAccessJoinColumn {
private List<BookPropertyAccessJoinColumn> books;

@OneToMany
@JoinColumn(name = "author_id")
public List<BookPropertyAccessJoinColumn> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccessJoinColumn {
// No reference back
}

// Property-access: compliant with @JoinTable
@Entity
class AuthorPropertyAccessJoinTable {
private List<BookPropertyAccessJoinTable> books;

@OneToMany
@JoinTable(name = "author_books2")
public List<BookPropertyAccessJoinTable> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccessJoinTable {
// No reference back
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
package checks;

import java.util.List;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;

public class OneToManyMappingCheckSampleJavax {

@Entity
class Author {
@OneToMany // Noncompliant {{Add "mappedBy" or "@JoinColumn" to this "@OneToMany" relationship.}}
// ^^^^^^^^^^
private List<Book> books;
}

@Entity
class Book {
@ManyToOne
private Author author;
}

// Compliant: uses mappedBy
@Entity
class AuthorWithMappedBy {
@OneToMany(mappedBy = "author")
private List<BookWithAuthor> books;
}

@Entity
class BookWithAuthor {
@ManyToOne
@JoinColumn(name = "author_id")
private AuthorWithMappedBy author;
}

// Compliant: uses @JoinColumn on the @OneToMany field
@Entity
class AuthorWithJoinColumn {
@OneToMany
@JoinColumn(name = "author_id")
private List<BookNoRef> books;
}

@Entity
class BookNoRef {
// No reference back to Author
}

@Entity
class AuthorUnidirectional {
@OneToMany // Noncompliant
private List<AnotherBook> books;
}

@Entity
class AnotherBook {
// No reference back
}

// Compliant: uses @JoinTable (explicit join table mapping)
@Entity
class AuthorWithJoinTable {
@OneToMany
@JoinTable(name = "author_books")
private List<BookWithJoinTable> books;
}

@Entity
class BookWithJoinTable {
// No reference back
}

// Property-access: noncompliant getter
@Entity
class AuthorPropertyAccess {
private List<BookPropertyAccess> books;

@OneToMany // Noncompliant
public List<BookPropertyAccess> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccess {
// No reference back
}

// Property-access: compliant with mappedBy
@Entity
class AuthorPropertyAccessMappedBy {
private List<BookPropertyAccessMappedBy> books;

@OneToMany(mappedBy = "author")
public List<BookPropertyAccessMappedBy> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccessMappedBy {
@ManyToOne
private AuthorPropertyAccessMappedBy author;
}

// Property-access: compliant with @JoinColumn
@Entity
class AuthorPropertyAccessJoinColumn {
private List<BookPropertyAccessJoinColumn> books;

@OneToMany
@JoinColumn(name = "author_id")
public List<BookPropertyAccessJoinColumn> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccessJoinColumn {
// No reference back
}

// Property-access: compliant with @JoinTable
@Entity
class AuthorPropertyAccessJoinTable {
private List<BookPropertyAccessJoinTable> books;

@OneToMany
@JoinTable(name = "author_books2")
public List<BookPropertyAccessJoinTable> getBooks() {
return books;
}
}

@Entity
class BookPropertyAccessJoinTable {
// No reference back
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* SonarQube Java
* Copyright (C) SonarSource Sàrl
* mailto:info AT sonarsource DOT com
*
* You can redistribute and/or modify this program under the terms of
* the Sonar Source-Available License Version 1, as published by SonarSource Sàrl.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the Sonar Source-Available License for more details.
*
* You should have received a copy of the Sonar Source-Available License
* along with this program; if not, see https://sonarsource.com/license/ssal/
*/
package org.sonar.java.checks;

import java.util.List;
import java.util.Set;
import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.AnnotationTree;
import org.sonar.plugins.java.api.tree.AssignmentExpressionTree;
import org.sonar.plugins.java.api.tree.IdentifierTree;
import org.sonar.plugins.java.api.tree.MethodTree;
import org.sonar.plugins.java.api.tree.Tree;
import org.sonar.plugins.java.api.tree.VariableTree;

@Rule(key = "S8948")
public class OneToManyMappingCheck extends IssuableSubscriptionVisitor {

private static final Set<String> ONE_TO_MANY_ANNOTATIONS = Set.of(
"jakarta.persistence.OneToMany",
"javax.persistence.OneToMany");

private static final Set<String> JOIN_COLUMN_ANNOTATIONS = Set.of(
"jakarta.persistence.JoinColumn",
"javax.persistence.JoinColumn");

private static final Set<String> JOIN_TABLE_ANNOTATIONS = Set.of(
"jakarta.persistence.JoinTable",
"javax.persistence.JoinTable");

@Override
public List<Tree.Kind> nodesToVisit() {
return List.of(Tree.Kind.VARIABLE, Tree.Kind.METHOD);
}
Comment thread
gitar-bot[bot] marked this conversation as resolved.

@Override
public void visitNode(Tree tree) {
var annotations = tree.is(Tree.Kind.METHOD)
? ((MethodTree) tree).modifiers().annotations()
: ((VariableTree) tree).modifiers().annotations();
checkAnnotations(annotations);
}

private void checkAnnotations(List<AnnotationTree> annotations) {
annotations.stream()
.filter(OneToManyMappingCheck::isOneToManyAnnotation)
.filter(annotation -> !hasMappedBy(annotation))
.filter(annotation -> annotations.stream().noneMatch(OneToManyMappingCheck::isJoinColumnAnnotation))
.filter(annotation -> annotations.stream().noneMatch(OneToManyMappingCheck::isJoinTableAnnotation))
.forEach(annotation -> reportIssue(annotation, "Add \"mappedBy\" or \"@JoinColumn\" to this \"@OneToMany\" relationship."));
Comment thread
gitar-bot[bot] marked this conversation as resolved.
}

private static boolean isOneToManyAnnotation(AnnotationTree annotation) {
return ONE_TO_MANY_ANNOTATIONS.stream().anyMatch(annotation.annotationType().symbolType()::is);
}

private static boolean isJoinColumnAnnotation(AnnotationTree annotation) {
return JOIN_COLUMN_ANNOTATIONS.stream().anyMatch(annotation.annotationType().symbolType()::is);
}

private static boolean isJoinTableAnnotation(AnnotationTree annotation) {
return JOIN_TABLE_ANNOTATIONS.stream().anyMatch(annotation.annotationType().symbolType()::is);
}

private static boolean hasMappedBy(AnnotationTree annotation) {
return annotation.arguments().stream()
.filter(arg -> arg.is(Tree.Kind.ASSIGNMENT))
.map(AssignmentExpressionTree.class::cast)
.map(AssignmentExpressionTree::variable)
.filter(v -> v.is(Tree.Kind.IDENTIFIER))
.map(IdentifierTree.class::cast)
.anyMatch(id -> "mappedBy".equals(id.name()));
}
}
Loading
Loading