Spring Boot services · Chapter 20 of 41

Entity relationships

By the end of this chapter you can link entities to each other (many to one, one to many and many to many), say which side owns a link, and answer with a DTO instead of a web of entities. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Data
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

Until now every table in Tapri Cloud stood alone. The counters ask questions that cross tables: who placed order 412, and what was on it? Anjali wants the menu tagged (hot, sweet, bestseller) so the app can filter it.

You could store a customerId number in Order and look the customer up by hand each time, and keep a list of line ids somewhere. Then nothing stops an id pointing at nothing, every screen repeats the lookups, and Hibernate cannot load an order together with what belongs to it.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Order.java: link every order to its customer

    Replace the @Transient on Order.customer with a many-to-one mapping whose foreign key column is orders.customer_id. An order saved with its customer, then loaded back with findById, has that customer (Asha), and the order's customer_id column holds the customer's id.

  2. Task 2

    Order.java: addLine keeps both ends in step

    Write Order.addLine(item, quantity, price) so it creates a new OrderLine for this order with that item, quantity and price, adds it to this order's lines, and returns it. After two calls, getLines() holds exactly the two lines in the order they were added, and each line's getOrder() is this same order. Nothing is saved: this is all in memory.

  3. Task 3 · interview question

    Interview: make OrderLine the owning side, with mappedBy on the order's lines

    Map OrderLine.order as a many-to-one on the column order_lines.order_id, and Order.lines as its other end with mappedBy, replacing both @Transient annotations. Lines saved with only their order set then load with the order (Cutting chai and Parle-G), order_lines has two rows with that order_id, and there is no join table between orders and lines (no table whose name starts with ORDERS_).

    Be ready to explain which side owns a two-way relationship and what mappedBy changes.

  4. Task 4

    MenuItem.java: tag menu items many to many

    Replace the @Transient on MenuItem.tags with a many-to-many mapping through a join table named menu_item_tags, with the columns menu_item_id and tag_id. Tags added to an item's set and saved with the item load back with it (Masala chai has hot and sweet, Bun maska has sweet), and menu_item_tags holds one row per pair: 2 rows for sweet.

  5. Task 5

    OrderController.java: answer GET /orders/{id} with a DTO

    Write OrderReader.view(id) so GET /orders/{id} answers 200 with an OrderView: the order's id and station, the customer's name as customer, one LineView (item, quantity, price) per line, and total, the sum of quantity times price over the lines (2 x 40 + 1 x 25 = 105). No line may carry its order.

    An id with no order answers 404 Not Found. Needs tasks 1 and 3, so the customer and the lines load.

What you will learn

  • @ManyToOne: many rows point at one, through a foreign key column.
  • @OneToMany(mappedBy): the other end of the same link, and which side owns it (the interview question).
  • @ManyToMany: a join table with one row per pair.
  • DTOs for linked entities: why an entity graph should not reach Jackson.

More in Data

Build "Entity relationships" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.