Hi there is there a reason why Hibernate restricts you to use the target table’s ID column/s when creating a one-to-one or many-to-one relationship?
In my case I’ve got an existing legacy database, which does not have foreign keys defined and due to impacts on other legacy applications I cannot have them created due to impacts.
Now let’s say I’ve got a table called Owners, which has a primary key that is made up of:
-
name
-
surname
-
social_security_number
But this table also has a unique constraint that is made up of:
- owner_id_number
Now I have another table called Vehicles that has an owner_id_number column (but is not defined as a foreign key of the other table) and I’d like to define a Many-To-One relationship using this column, as I know that I will have uniqueness due to the unique constraint in the Owners table, but when I do so I get the following error because I’m not using the Owners entity ID columns in the JoinColumn annotation reference.
Caused by: org.hibernate.AnnotationException: referencedColumnName(OWNER_ID_NUMBER) of com.john.doe.data.api.models.Vehicles.owner referencing com.john.doe.data.api.models.Owners not mapped to a single property
I also confirm that OWNER_ID_NUMBER is defined as a unique constraint in the Owners entity using the table level constraint UniqueConstraint annotation.
Is there a way to achieve what I need given the limitations of my legacy database?