JPA @OneToMany with composite primary keys

Solution for JPA @OneToMany with composite primary keys
is Given Below:

I need to map with JPA the following legacy DB table structure I cannot change.
It’s a one to many relationship between table ao_rda_acq (1) -> ao_rda_acq_righe (many) (purchase requisition -> purchase requisition rows)

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 many

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     
)

The primary key of table ao_rda_acq side one of the relationship has 3 fields id_divisione, esercizio, id_rda. The primay key of the table side many of the relationship has the same 3 filed plus a 4th field nr_riga.

I tryed with this JPA approch using @IdClass annotation for composite primary keys

@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
}

where

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

The entity for the rows is

@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
}

where

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

This code compiles but JPA at start-up complains with this message

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]

Is this approch the best one to map my unhappy DB structure? If so what did I miss?

When you use @Id without the @Column annotation, the name of the column is assumed to be the name of the annotated property.
Given that your DB column seems to be *id_divisione* you need to use also the annotation @Column(name = "id_divisione").

This also applies to the other properties annotated with @Id.