Hi,
I receive some stacktrace during startup after modelling my entities: org.hibernate.MappingException: invalid join columns for association: provider.debtor
The simplified model is
@Entity
public class Debtor {
...
private Provider provider;
@OneToOne(fetch = LAZY)
@JoinColumns({@JoinColumn(name = "provider_id", referencedColumnName = "outer_provider_id"),
@JoinColumn(name = "location", referencedColumnName = "location")})
public Provider getProvider() {
return provider;
}
}
@Entity
public class Provider {
...
private Debtor debtor;
@OneToOne(mappedBy = "provider")
public Debtor getDebtor() {
return debtor;
}
}
There are other Entities having a Provider relationship - functional - with the same join columns definition, but they are all ManyToOne. Is there a rule unknown to me about using JoinColumns with OneToOne?
I debugged Hibernate and it seems to have a problem resolving the join at:
The problem might be caused because the Provider entity has a single-column identifier (e.g. provider_id) while you try to reference this entity by 2 columns (e.g. provider_id and location).
Since the provider_id is unique, why do you want to add the extra column to locate the Provider from the Debtor entity?
If you want to reference the @OneToOne association by a non-identifier parent property or combination of properties, you can use @JoinFormula.
Not quite, the table PROVIDER has a primary key, but in the entity relationship diagram it is not used as the foreign key, instead the people before me (may they roast in a very special hell for IT abuse) have used two other columns as a natural key to link the tables. The database is certainly older than Hibernate and JDBC and they had a concept that all database access had to be done through stored procedure calls.
Anyway, almost all relations to the provider table use two columns as foreign key, none of which is the primary key.
In the ManyToOne relationships this works in JPA/Hibernate, but the OneToOneType seems to be hardcoded to use the PK as LHS join, not any JoinTable annotation.
I think it’s not currently possible to reference an association without using the identifier. So, if the @JoinColumns don’t match the parent identifier, then relationship will not work unless adding support for it in Hibernate.
Would you like to investigate it and provide a Pull Request with a fix proposal?