JPA @JoinColumn issues while joining on non primary key columns

16,794
  1. JPA does not allow references to non-PK columns.

  2. ReferencedColumn property is used to specify join column, when there is a composite PK in referenced table.

How can I make this work??

Normalize your database to use common PK-FK relationships. If needed - define composite PK.

Share:
16,794

Related videos on Youtube

Bhumir Jhaveri
Author by

Bhumir Jhaveri

Working as Software engineer III at Ebay inc.,

Updated on June 04, 2022

Comments

  • Bhumir Jhaveri
    Bhumir Jhaveri almost 2 years

    I want to connect table based on one direction join, here is my code:

    Class Person {
    
    @id
    String person_sk;
    
    int person_id;
    
    String Person_name;
    
    @OneToMany
    @joinColumn (name="person_reference_id")
    List<address> getAddresses() {}
    
    }
    
    class Address
    {
    
    @id
    int person_reference_id (referred from Person);
    
    @id
    int address_id;
    
    @id
    int phone_id;
    
    String street_name, zip_code;
    
    }
    

    Now when I do getAddress, it does not work, because my join is based on person_ref_id and @id (primaryKey) column in Person class is person_sk.

    If I use referencedColumn then also it doesn't work.

  • Bhumir Jhaveri
    Bhumir Jhaveri almost 12 years
    well thats correct, but its really strange why framework is abandoning you to implement such odd situation.
  • Bhumir Jhaveri
    Bhumir Jhaveri almost 12 years
    its true, but this cant be modified since its coming from certain external apps, but this is not appropriate functionality supported by jpa framework
  • Bhumir Jhaveri
    Bhumir Jhaveri almost 12 years
    All this is good when it's ideal scenario, but everytime one dont end up in getting ideal scenario and there are hardly any changes that can be applied to the data model.
  • d1e
    d1e almost 12 years
    I think it is good they do not support such ridiculous stuff. It is just that if they were to workaround every feature concerning other layers, it would be over-engineered. Here is a workaround you can try stackoverflow.com/a/1506775/685962
  • cbmeeks
    cbmeeks over 8 years
    Telling someone to normalize their database does not work when you have legacy databases that actually pre-date Hibernate and contain millions upon millions of records and 15 years of legacy business logic running on dozens of servers.