Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between name and referencedColumnName in @JoinColumn annotation?

@ManyToOne
@JoinColumn(name = "someValue" , referencedColumnName = "someOtherValue" )

What values are to be placed in name and referencedColumnName column if 2 tables are linked by ManyToOne association?

like image 399
Sarthak Avatar asked Nov 13 '18 18:11

Sarthak


People also ask

What is referencedColumnName in @JoinColumn Hibernate?

Quoting API on referencedColumnName: The name of the column referenced by this foreign key column. Default (only applies if single join column is being used): The same name as the primary key column of the referenced table.

What is name in @JoinColumn?

The @JoinColumn annotation combined with a @OneToOne mapping indicates that a given column in the owner entity refers to a primary key in the reference entity: @Entity public class Office { @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "addressId") private Address address; }

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. (Optional) The SQL fragment that is used when generating the DDL for the column.

What is difference between MappedBy and @JoinColumn?

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.


2 Answers

Suppose you have Two tables:

1. Department table with columns:
 a. Dept_ID (primary key)
 b. Dept_Name

2. Employee Table with following column:
  a. Emp_Id (primary key)
  b. Dept_Id_fk (foreign key)
  c. Salary

Now your join column for Employee Entity class will be

@ManyToOne
@JoinColumn(name = "Dept_Id_fk", referencedColumnName = "Dept_ID")
Department department;

So referencedColumnName means column that you are referencing to from your foreign key column.

like image 82
Sayantan Mandal Avatar answered Oct 23 '22 22:10

Sayantan Mandal


How would the join column look like for oneToMany association? Here is an example:

Person table:

person_id (pk), person_name

person_reltn table:

person_reltn_id (pk), child_person_id (fk), parent_person_id (fk)

For the above tables, if I were to create the Person entity:

 class Person(){
    
     @Id
      @Column(name = "PERSON_ID")
      private long personId;
    
      @NotFound(action = NotFoundAction.IGNORE)
      @OneToMany(fetch = FetchType.LAZY)
      @JoinColumn(
          name = "CHILD_PERSON_ID",
          referencedColumnName = "PERSON_ID",
          updatable = false,
          insertable = false)
      @Where(clause = "ACTIVE_IND = 1")
      @Filter(
          name = FilterConstants.END_EFFECTIVE_DATE_TIME_FILTER,
          condition = FilterConstants.END_EFFECTIVE_DATE_TIME_CONDITION)
      @Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
      private final List<PersonRelation> personRelations = new ArrayList<>();
}

In the joinColumn, should the name always have the foreign key (which means the value from entity you are joining to) and the referenceColumnName should alway have the primary key on the entity? If yes, it will be the opposite of Sayantan's response above. Please let me know if I misunderstood the concept.

Update on 03/04/2021

After doing more research, I found the documentation on how to set the referenceColumnName based on the entity mappings[1]. Looks like, for unidirectional OneToMany mapping, the referenced column is in the table of the source entity.

1.https://docs.jboss.org/hibernate/jpa/2.1/api/javax/persistence/JoinColumn.html

like image 1
user1555543 Avatar answered Oct 23 '22 23:10

user1555543