具有复合主键的 JPA @OneToMany

JPA @OneToMany with composite primary keys

我需要使用 JPA 映射以下遗留数据库 table 我无法更改的结构。 tableao_rda_acq(1)->ao_rda_acq_righe(多)(采购申请->采购申请行)

是一对多的关系

Table (1)

create table ao_rda_acq
(
    id_divisione     varchar(4)  not null,
    esercizio        smallint    not null,
    id_rda           varchar(10) not null,
    ...
    other fields
    ...
    constraint pk_ao_rda_acq
        primary key (id_divisione, esercizio, id_rda)   
)   

table很多

create table ao_rda_acq_righe
(
    id_divisione     varchar(4) ,
    esercizio        smallint    not null,
    id_rda           varchar(10) not null,
    nr_riga          integer     not null,
    ...
    other fields
    ... 
    constraint pk_ao_rda_righe
        primary key (id_divisione, esercizio, id_rda, nr_riga),
    constraint ao_rda_acq_righe_ao_rda_acq_id_divisione_esercizio_id_rda_fk
        foreign key (id_divisione, esercizio, id_rda) references ao_rda_acq     
)

table ao_rda_acq 侧关系的主键有 3 个字段 id_divisione、esercizio、id_rda。 table 侧的主键许多关系具有相同的第 3 个字段加上第 4 个字段 nr_riga。

我尝试使用此 JPA 方法对复合主键使用 @IdClass 注释

@Table(name="ao_rda_acq")
@Entity
@IdClass(RdaId.class)
@JsonIgnoreProperties(ignoreUnknown = true)
public class Rda {

    @Id
    public String idDivisione;
    @Id
    public Integer esercizio;
    @Id
    public String idRda;
    
    @OneToMany(
            cascade = CascadeType.ALL,
            orphanRemoval = true
    )
    @JoinColumns({
        @JoinColumn(name="id_divisione", referencedColumnName = "id_divisione"),
        @JoinColumn(name="esercizio", referencedColumnName = "esercizio"),
        @JoinColumn(name="id_rda", referencedColumnName = "id_rda")
    })
    @OrderBy("nrRiga")
    public List<RdaRiga> righe = new ArrayList<>();     

    //Additional fields
}

哪里

public class RdaId implements Serializable {
    String idDivisione;
    Integer esercizio;
    String idRda;
}

行的实体是

@Entity
@JsonIgnoreProperties(ignoreUnknown = true)
@IdClass(RdaRigaId.class)
@Table(name = "ao_rda_acq_righe")
public class RdaRiga {

    @Id
    public String idDivisione;
    @Id
    public Integer esercizio;
    @Id
    public String idRda;
    @Id
    public Long nrRiga; 
    
    //More fields
}

哪里

public class RdaRigaId implements Serializable {
    String idDivisione;
    Integer esercizio;
    String idRda;
    Long nrRiga;
}

此代码可以编译,但 JPA 在启动时会抱怨此消息

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is org.hibernate.DuplicateMappingException: Table [ao_rda_acq_righe] contains physical column name [id_divisione] referred to by multiple logical column names: [id_divisione], [idDivisione]

这种方法是映射我不满意的数据库结构的最佳方法吗?如果是这样,我错过了什么?

当您在没有 @Column 注释的情况下使用 @Id 时,列的名称被假定为注释 属性 的名称。 鉴于您的数据库列似乎是 *id_divisione* 您还需要使用注释 @Column(name = "id_divisione").

这也适用于用 @Id 注释的其他属性。