Multiple JoinColumns pointing to the same non-pk columns

67 views Asked by At

Imagine the following entities

EntityA (the combination of attr1, attr2 is unique){
    - pk
    ...
    - attr1
    - attr2
    ...
    other unique attributes
}
 EntityB (the combination of attr1, attr2 is unique) {
    - pk
    ...
    - attr1
    - attr2
    ...
    other unique attributes
}
 EntityC (the combination of attr1, attr2 is not unique){
    - pk
    ...
    - attr1
    - attr2
    ...
    other unique attributes
}

attr1 and attr2 fields are the same type on all 3 entities. On Entity1 I would like to create a JoinColumns based join for both Entity1 and Entity2 as follows:

EntityA (the combination of attr1, attr2 is unique){
    - pk
    ...
    - attr1
    - attr2
    ...
    other unique attributes
    ...
    @OneToOne(fetch = FetchType.LAZY)        
    @JoinColumns(
            value = {
                    @JoinColumn(
                            name = "attr1", referencedColumnName = "attr1",
                            updatable = false, insertable = false),
                    @JoinColumn(
                            name = "attr2", referencedColumnName = "attr2",
                            updatable = false, insertable = false)
            })
    EntityB entityB;
    
    @OneToMany(fetch = FetchType.LAZY)
    @JoinColumns(
            value = {
                    @JoinColumn(
                            name = "attr1", referencedColumnName = "attr1",
                            updatable = false, insertable = false),
                    @JoinColumn(
                            name = "attr2", referencedColumnName = "attr2",
                            updatable = false, insertable = false)
            })
    List<EntityC> entityCList;
}

With the definition above, I get the following error:

Referenced column 'attr2' mapped by target property 'entityB' occurs out of order in the list of '@JoinColumn's

If I remove the definition of either entityB or entityB, the remaining one works properly. (Note: my goal with the non-bidirectional relationship is only to be able to fetch the necessary related objects, nothing more, that is why I am trying to define it this way instead of a separate query, so I can directly get the data via the getters.)

0

There are 0 answers