This is an issue when a one-to-many relationship in object-relational mapping is declared without either specifying which side owns the bidirectional relationship or providing explicit foreign key column configuration, causing the ORM framework to create an unnecessary join table.

In JPA, this specifically refers to the @OneToMany annotation without a mappedBy attribute or @JoinColumn annotation.

Why is this an issue?

When you define a one-to-many relationship in an object-relational mapping framework without specifying how the relationship should be mapped, the persistence provider follows its specification default: it creates a separate join table to represent the association.

For example, if you have an Author entity with a collection of Book entities:

Entity: Author
  one-to-many relationship -> books: collection of Book

The ORM will create three tables: Author, Book, and Author_Book (the join table). The join table contains two foreign key columns: one referencing Author and one referencing Book.

This default behavior is rarely what you want for true one-to-many relationships

In JPA, this occurs when you use the @OneToMany annotation without additional mapping configuration. For example:

@Entity
public class Author {
    @OneToMany
    private List<Book> books;
}

What is the potential impact?

Using the default join table mapping for one-to-many relationships in object-relational mapping frameworks degrades application performance through additional database tables, extra SQL statements, and more complex query plans. It also makes the database schema less intuitive and harder to maintain, as the foreign key is not stored where developers would naturally expect it.

In Java, this specifically refers to the @OneToMany annotation in JPA (Java Persistence API) and Hibernate.

How to fix it

For bidirectional relationships (where the child entity has a reference back to the parent), use the mappedBy attribute on the @OneToMany side to indicate that the relationship is owned by the child entity. This tells JPA to use the foreign key column on the child table. The mappedBy value must match the field name in the child entity that references the parent.

For unidirectional relationships (where only the parent knows about the children), use @JoinColumn to specify that the foreign key should be stored in the child table. The @JoinColumn annotation tells JPA to create or use a foreign key column in the child table instead of creating a join table.

Code examples

Noncompliant code example

@Entity
public class Author {
    @OneToMany // Noncompliant
    private List<Book> books;
}

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

Compliant solution

@Entity
public class Author {
    @OneToMany(mappedBy = "author")
    private List<Book> books;
}

@Entity
public class Book {
    @ManyToOne
    @JoinColumn(name = "author_id")
    private Author author;
}

Noncompliant code example

@Entity
public class Author {
    @OneToMany // Noncompliant
    private List<Book> books;
}

@Entity
public class Book {
    // No reference back to Author
}

Compliant solution

@Entity
public class Author {
    @OneToMany
    @JoinColumn(name = "author_id")
    private List<Book> books;
}

@Entity
public class Book {
    // No reference back to Author
    // JPA will add author_id column to Book table
}

Resources

Documentation