2

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.

abhi
  • 1,760
  • 1
  • 24
  • 40
Bhumir Jhaveri
  • 21
  • 1
  • 1
  • 3

3 Answers3

0
  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.

Community
  • 1
  • 1
d1e
  • 6,372
  • 2
  • 28
  • 41
  • well thats correct, but its really strange why framework is abandoning you to implement such odd situation. – Bhumir Jhaveri Jul 09 '12 at 23:25
  • 1
    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. – Bhumir Jhaveri Jul 09 '12 at 23:27
  • 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 http://stackoverflow.com/a/1506775/685962 – d1e Jul 10 '12 at 06:50
  • 9
    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. – cbmeeks Aug 31 '15 at 21:27
  • this is useless advice if you can't change your data model ffs – Enerccio Nov 11 '22 at 13:02
  • Did you manage to resolve the problem? – d1e Nov 30 '22 at 07:17
0

You should use right @Id annotation on the joined Entity(table) property(column), which you want to join by. The best solution is to normalize the tables in the DB, and make the appropriate changes in your Entities.

@Data
@Entity
@Table(name = "person")
class Person {

    @Id
    String person_sk;

    int person_id;

    String Person_name;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "person_sk")
    List<address> getAddresses() {}
}

@Data
@Entity
@Table(name = "address")
class Address {

    @Id
    int person_reference_id;

    int address_id;

    int phone_id;

    String street_name, zip_code;
}
zdrsoft
  • 2,417
  • 19
  • 10
-2

Unusual data model, you may want to rethink it.

Also, normally it is best to define a @OneToMany using a mappedBy and an inverse @ManyToOne.

JPA does not directly support your data model, but if you are using EclipseLink, you can define the OneToManyMapping using a DescriptorCustomizer and use whatever foreign keys or join conditions you require.

James
  • 17,965
  • 11
  • 91
  • 146
  • 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 Jul 09 '12 at 23:26