Scrivio Interview library
DebuggingFoundations6 min read · updated 12 November 2026

A unidirectional @OneToMany private List<OrderLine> lines on Order (no mappedBy, no @JoinColumn) produces an extra table order_lines and several statements per insert. Why?

Without mappedBy and without @JoinColumn, JPA maps a unidirectional @OneToMany with a join table by default, so Hibernate creates a link table and, for each new line, inserts the line and then inserts a link row; when the list changes it may delete and re-insert all link rows. Adding @JoinColumn(name = "order_id") moves the link into a foreign key in the child table, although Hibernate may still send extra UPDATE statements to set it. The usual best mapping is bidirectional, with @ManyToOne on OrderLine as the owner.

Why this question is asked

This surprises many developers the first time they look at the generated schema: they wanted a simple foreign key from order_line to orders and got a third table instead. Then the SQL log shows three or four statements for what should be one insert. Interviewers ask about it because it tests whether we know JPA's default mapping rules and the cost of a unidirectional collection. In this answer, we will recap what unidirectional means, see why JPA chooses a join table, look at the statements Hibernate sends, try the @JoinColumn fix and its limits, and end with the mapping we would normally choose.

1A Quick Recap: Unidirectional Collections

A unidirectional association is one that only one entity knows about. Here Order has a List<OrderLine> lines, and OrderLine has no reference back to Order. In Java that is perfectly natural. In the database, though, the relationship still has to be stored somewhere, and the line, which is the natural place for a foreign key, has no field that represents it.

So JPA has a choice to make. Without a hint from us, the specification's default for a unidirectional one-to-many is a join table, a separate table with two foreign key columns, one to the order and one to the line. That is the extra order_lines table in the question (its exact name depends on the naming strategy).

2Watching the Statements

Here is the mapping from the question and what saving an order with two lines produces.

Java
@Entity class Order {
@Id @GeneratedValue Long id;
@OneToMany(cascade = CascadeType.ALL) // no mappedBy, no @JoinColumn
List<OrderLine> lines = new ArrayList<>();
}
order.getLines().add(new OrderLine("BOOK-1"));
order.getLines().add(new OrderLine("PEN-7"));
em.persist(order);
// insert into orders (id) values (1)
// insert into order_line (sku, id) values ('BOOK-1', 1)
// insert into order_line (sku, id) values ('PEN-7', 2)
// insert into orders_lines (order_id, lines_id) values (1, 1)
// insert into orders_lines (order_id, lines_id) values (1, 2)
Statements for an order with 30 lines
OperationJoin table (default)Bidirectional, child owns FK
Insert order with 30 lines1 + 30 + 30 inserts1 + 30 inserts
Load linesjoin of 3 tablesjoin of 2 tables
Remove one linedelete all links, re-insert 291 delete
Extra tableyesno
The default mapping multiplies statements as the collection grows.

Notice that each line costs two inserts, one for the line and one for the link. Reads cost more too: loading the lines now joins three tables instead of two.

Changes to the list are worse. A List without an order column is a bag, so Hibernate has no way to identify one link row by position. When we remove a line, Hibernate may delete all link rows for the order and re-insert the remaining ones. For an order with 30 lines, removing one can mean 1 delete and 29 inserts in the join table.

3Fix 1: @JoinColumn on the Collection

We can tell JPA to store the relationship as a foreign key in the child table instead, by adding @JoinColumn to the collection. The join table disappears.

Java
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "order_id") // foreign key in order_line
List<OrderLine> lines = new ArrayList<>();
em.persist(order);
// insert into orders (id) values (1)
// insert into order_line (sku, id) values ('BOOK-1', 1)
// insert into order_line (sku, id) values ('PEN-7', 2)
// update order_line set order_id = 1 where id = 1
// update order_line set order_id = 1 where id = 2

That is better, but there is a catch. The OrderLine entity has no field for order_id, so when Hibernate inserts a line it does not, by default, treat the column as part of the line's own state. It inserts the line first and then updates the foreign key as a separate step that belongs to the collection. Declaring the join column nullable = false lets Hibernate include it in the insert in many cases, which removes the updates; whether it does depends on the version and mapping, so check the SQL log. Removal also needs care: without orphanRemoval, removing a line from the list only sets its order_id to null.

4Fix 2: The Usual Best Choice, a Bidirectional Mapping

The cleanest solution is to give OrderLine a @ManyToOne to Order and make the collection the inverse side with mappedBy. Now the foreign key is part of the line's own state, Hibernate fills it in the insert, and removing a line with orphanRemoval issues exactly one delete.

Java
@Entity class OrderLine {
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "order_id") Order order;
}
@Entity class Order {
@OneToMany(mappedBy = "order", cascade = CascadeType.ALL, orphanRemoval = true)
List<OrderLine> lines = new ArrayList<>();
void addLine(OrderLine l) { lines.add(l); l.order = this; }
}
// insert into orders ...; insert into order_line (order_id, sku, id) values (1, 'BOOK-1', 1); ...

The price is a reference from the line to its order and a helper method to keep both sides in step. That is a small cost for one insert per line and no extra table. If we really want only one direction, a unidirectional @ManyToOne from OrderLine to Order, with lines queried through a repository, is also efficient; it is the unidirectional @OneToMany that is expensive.

5Follow-up questions

Is a join table ever the right choice for one-to-many?

Yes, when the child table must not know about the parent, for example a shared Attachment table used by orders, invoices and tickets. A join table per owner keeps the attachment table clean. That is a deliberate design choice, not an accident of defaults.

Would an @OrderColumn help the join table version?

It gives the list an index column, so Hibernate can update or delete a single row by position instead of rewriting the collection. Removing from the middle still shifts later indexes, so it trades the rewrite for index updates.

How do we notice this early?

By reading the generated DDL and the SQL log in tests, and by asserting statement counts in integration tests. An unexpected third table in the schema review is the first clue.

6Conclusion

In this answer, we saw that a unidirectional @OneToMany without mappedBy or @JoinColumn uses a join table by default, which costs an extra insert per child, three-table joins, and full rewrites of the links when the bag changes. @JoinColumn removes the join table but may still cause extra updates because the child does not own the column. The usual best mapping is bidirectional, with @ManyToOne on the child as the owner, so the foreign key is written once in the insert.

What to say in the interviewAbout 80 seconds aloud

Open with

That's JPA's default mapping for a unidirectional one-to-many. If there's no mappedBy and no @JoinColumn, the spec says to use a join table, so we get a third table with an order_id and a lines_id column.

Then, if they want more

The child entity has no field for the relationship, so the foreign key can't live in order_line by default. That's why each new line costs two inserts, one for the line and one for the link row.

And because a List without an order column is a bag, Hibernate can't identify a single link row, so when the list changes it may delete all links for the order and re-insert the rest. One fix is to add @JoinColumn(name = "order_id") on the collection.

That removes the join table and puts a foreign key in order_line, but since the line itself doesn't own that column, Hibernate often inserts the line and then issues an update to set order_id; making the join column non-nullable can help, but I'd check the SQL.

The approach I'd normally use is bidirectional: OrderLine gets a lazy @ManyToOne with the join column and is the owning side, and Order's collection uses mappedBy. Then there's one insert per line with the foreign key already set, and removing a line is a single delete.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A unidirectional @OneToMany private List<OrderLine> lines on Order (no mappedBy, no...": https://scrivio-interview-library.netlify.app/databases/jpa-relationships/c01ea9d8ed/