How to establish a two-way one-to-many relationship between a composite restriction of fields?

13 Views Asked by At

How to establish a two-way one-to-many relationship between a composite restriction of fields from a composite primary key in hibernate?

Hi friends, help me solve the problem. There is a table one and its 4 fields are a composite primary key and there is a second table in which the 4th fields are a composite primary key. The second table has a composite foreign key to a composite unique constraint of 3 fields that are part of a composite primary key. I need help in properly linking entities in hibernate

Here tables

CREATE TABLE table_1
(
    field_1_t1_pk varchar not null,
    field_2_t1_pk varchar not null,
    field_3_t1_pk varchar not null,
    field_4_t1_pk varchar not null,
    field_5_t1 varchar not null,
    field_6_t1 varchar not null,

    PRIMARY KEY (field_1_t1_pk, field_2_t1_pk, field_3_t1_pk, field_4_t1_pk),
    CONSTRAINT table_1_unique_constraint UNIQUE (field_1_t1_pk, field_2_t1_pk, field_3_t1_pk)
);

CREATE TABLE table_2
(
    field_1_t2_pk     varchar not null,
    field_2_t2_pk     varchar not null,
    field_3_t2_pk     varchar not null,
    field_4_t2_pk    varchar not null,
    field_5_t2 varchar not null,
    field_6_t2 varchar not null,
    PRIMARY KEY (field_1_t2_pk, field_2_t2_pk, field_3_t2_pk, field_4_t2_pk),
    FOREIGN KEY (field_1_t2_pk, field_2_t2_pk, field_3_t2_pk) REFERENCES table_1 (field_1_t1_pk, field_2_t1_pk, field_3_t1_pk)
);

Here entities

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "table_1")
public class TableOne{

    @EmbeddedId
    private TableOnePK tableOnePK;

    @Column(name = " field_5_t1")
    private String fieldFiveTableOne;

    @Column(name = " field_6_t1")
    private String fieldSixTableOne;

    @OneToMany(?)
    private List<TableTwo> tablesTwo = new ArrayList();
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
@ToString
@EqualsAndHashCode
public class TableOnePK implements Serializable {

    @Column(name = "field_1_t1_pk")
    private String fieldOneTableOnePK;

    @Column(name = "field_2_t1_pk")
    private UUID fieldTwoTableOnePK;

    @Column(name = " field_3_t1_pk")
    private Integer fieldThreeTableOnePK;
    @Column(name = " field_4_t1_pk")
    private String fieldFourTableOnePK;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Entity
@Table(name = "table_2")
public class TableTwo {
    @EmbeddedId
    private TableTwoPK tableTwoPK;

    @Column(name = " field_5_t2")
    private String fieldFiveTableTwo;

    @Column(name = " field_6_t2")
    private String fieldSixTableTwo;

    @ManyToOne
    private TableOne tableOne;
}

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Embeddable
@ToString
@EqualsAndHashCode
public class TableTwoPK implements Serializable {

    @Column(name = "field_1_t2_pk")
    private String fieldOneTableTwoPK;

    @Column(name = "field_2_t2_pk")
    private UUID fieldTwoTableTwoPK;

    @Column(name = " field_3_t2_pk")
    private Integer fieldThreeTableTwoPK;
    @Column(name = " field_4_t2_pk")
    private String fieldFourTableTwoPK;
}

I tried using @MapsId, @JoinColumns, but I couldn't achieve the desired result - fell with an exception.

0

There are 0 best solutions below