Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is referencedColumnName used for in JPA?

In JPA there is an attribute called referencedColumnName that can be set on @JoinColumn, @PrimaryKeyJoinColumn what is the idea behind this setting, can someone give a good example of where this can be used?

like image 853
ams Avatar asked Jun 28 '12 12:06

ams


People also ask

What is the use of @JoinColumn in Hibernate?

You can use the @JoinColumn annotation to map the foreign key column of a managed association. The @PrimaryKeyJoinColumn specifies the mapping of the foreign key column of a secondary table or the foreign key column in an inheritance mapping that uses the JOINED strategy.

What is the use of @JoinColumn annotation?

Annotation Type JoinColumn. Specifies a column for joining an entity association or element collection. If the JoinColumn annotation itself is defaulted, a single join column is assumed and the default values apply.

What is the use of @JoinColumn in spring boot?

The @JoinColumn annotation helps us specify the column we'll use for joining an entity association or element collection. On the other hand, the mappedBy attribute is used to define the referencing side (non-owning side) of the relationship.

What is @JoinColumn in JPA?

JPA JAVA EE. @JoinColumn is used to specify a column for joining an entity association or element collection. This annotation indicates that the enclosing entity is the owner of the relationship and the corresponding table has a foreign key column which references to the table of the non-owning side.


2 Answers

It is there to specify another column as the default id column of the other table, e.g. consider the following

TableA   id int identity   tableb_key varchar   TableB   id int identity   key varchar unique  // in class for TableA @JoinColumn(name="tableb_key", referencedColumnName="key") 
like image 58
Firo Avatar answered Sep 25 '22 22:09

Firo


"referencedColumnName" property is the name of the column in the table that you are making reference with the column you are anotating. Or in a short manner: it's the column referenced in the destination table. Imagine something like this: cars and persons. One person can have many cars but one car belongs only to one person (sorry, I don't like anyone else driving my car).

Table Person
name char(64) primary key
age int

Table Car
car_registration char(32) primary key
car_brand (char 64)
car_model (char64)
owner_name char(64) foreign key references Person(name)

When you implement classes you will have something like

class Person{    ... }  class Car{     ...     @ManyToOne     @JoinColumn([column]name="owner_name", referencedColumnName="name")     private Person owner; } 

EDIT: as @searchengine27 has commented, columnName does not exist as a field in persistence section of Java7 docs. I can't remember where I took this property from, but I remember using it, that's why I'm leaving it in my example.

like image 42
EAmez Avatar answered Sep 25 '22 22:09

EAmez