0

First of all I would like to thank everyone reading this. I guess this question might have been asked before in this forum, but I couldn't find it, probably becouse I didn't know how to search for it. Any help would be very useful.

In my Java application's database, there are two tables that relate with each other using a many to many relationship. There is, of course, a link table between them.

Table 1: Modules
Columns: Id, name, etc

Table 2: Status
Columns: Id, name

Table 3: Module_status
Columns: Module_Id, Status_Id, Status_Value

When I set the many to many relationship in hibernate, the generated class Modules has a list of Status, but one of the informations I need is in the Module_status table, which is the value of this Status on the given Module.
Is there a way to configure the framework in a way that I can retrieve this information in the Module object?

1 Answers1

4

The Module_Status table is more than just a join table between the other two. It contains additional information. It's thus a functional table like all the other ones, which should be mapped as an entity. You would thus have

Module {
    @OneToMany
    Set<ModuleSatus>
}

ModuleStatus {
    @ManyToOne
    Module

    @ManyToOne
    Status
}

Status {
    @OneToMany
    Set<ModuleStatus>
}

To make the mapping and your life simpler, and your model clean, I really advise you to also add a single-column, auto-generated ID column in this Module_Status table, like for the other entities.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255