Skip to content
Advertisement

How to annotate Map with JPA?

The app’s stack: Spring MVC, Spring DataJPA, Hibernate. There are three entities: student, tutor, theme.

Theme:

JavaScript

Student:

JavaScript

Tutor:

JavaScript

For save student-tutor-theme relationships i want use this table (PostgreSQL):

JavaScript

How i can to annotate tutors and students fields in entities, for their content correct persists in this table?

Advertisement

Answer

Like @kolossus mentioned: Use the @MapKeyJoinColumn¹ annotation, so that the classes (or the map fields) look like this (you can ignore the extention of AbstractPersistable):

Student:

JavaScript

Tutor:

JavaScript

Given that, something like this would be created:

JavaScript

¹: See the Javadoc documentation of @MapKeyJoinColumn for some other samples

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