Skip to content
Advertisement

Connecting 3 tables with hibernate / spring-boot

I have problem understanding how to connect 3 tables with spring-boot / hibernate. Tables are: Users, Technologies, Categories

Every user has all of the 10 categories but inside this categories they can save one or more technologies. Each technology can be listed in several different categories.

I have a code that works partially as for now instead referencing the table category, I am just creating new category so I have duplicates in my BDD. Ideally I would love to have for each user data structure that like something like this (in pseudo-code):

JavaScript

my tables are:

USER TABLE

JavaScript

TECHNOLOGY

JavaScript

TECHNOLOGY CATEGORY:

JavaScript

and the table where I am trying to connect users with categories (from which each one has list of technologies)

USER_CATEGORIES

JavaScript

so for I have tried/read this: Joining three tables using MySQL

ManyToManyToMany – Joining three tables with Hibernate annotations

Hibernate: How to Join three 3 tables in one join table in Annotation?

Hibernate: mapping 3 tables

but with no success as every try to implement solutions above resulted in exceptions (all connected with hibernate unable to create tables) that I couldn’t resolve. Thank you

Advertisement

Answer

If I understoond correctly, something like this would work for you, assumed your example code works:

User class, which joins the categories:

JavaScript

Category entity, which contains the technology’s category, and the technologies:

JavaScript

TechnologyCategory entity:

JavaScript

Technology entity:

JavaScript

If you don’t want the ids to show up in your JSON, just put @JsonIgnore annotation above the id property.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement