Table with two related secondary tables

I am trying to may an entity with two associated tables, but I am not sure about this mapping aspect.

Suppose that I have Entity P and its information is mapped to a table P and to another two tables (S1 and S2).

public class P{
   atributeType p1;
   atributeType p2;
   atributeType s11;
   atributeType s12;
   atributeType s21;
   atributeType s22;
.....

}

The idea is that attributes p1 and p2 go to Table P; attributes s11 and s12 go to S1; while attributes s21 and s22 go to S2. Additionally, tables S1 and S2 are related by a foreign key (PK of S2 is at the same time a foreign key to PK of S2).

Does it have sense?

Thank you very much for your help.

728x90

1 Answers Table with two related secondary tables

Specifying secondary tables is work for @SecondaryTable, in case of multiple secondary tables with @SecondaryTables. Attribute table in @Column specifies table where column is located.

In this case mappings are then roughly as follows:

@SecondaryTables({
        @SecondaryTable(name = "S1"),
        @SecondaryTable(name = "S2")
})
@Entity
public class P{
    @Id
    int pk;
    int p1;
    int p2;

    @Column (table = "S1")
    int s11;

    @Column (table = "S1")
    int s12;

    @Column (table = "S1")
    int s21;

    @Column (table = "S1")
    int s22;

    //....

}

4 months ago