What is the difference between:
@Entity
public class Company {
@OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY)
@JoinColumn(name = "companyIdRef", referencedColumnName = "companyId")
private List<Branch> branches;
...
}
and
@Entity
public class Company {
@OneToMany(cascade = CascadeType.ALL , fetch = FetchType.LAZY, mappedBy = "companyIdRef")
private List<Branch> branches;
...
}
I'd just like to add that
@JoinColumn
does not always have to be related to the physical information location as this answer suggests. You can combine@JoinColumn
with@OneToMany
even if the parent table has no table data pointing to the child table.How to define unidirectional OneToMany relationship in JPA
Unidirectional OneToMany, No Inverse ManyToOne, No Join Table
It seems to only be available in
JPA 2.x+
though. It's useful for situations where you want the child class to just contain the ID of the parent, not a full on reference.As I explained in this article, if you use the
@OneToMany
annotation with@JoinColumn
, then you have a unidirectional association.If you use the
@OneToMany
with themappedBy
attribute set, you have a bidirectional association, meaning you need to have a@ManyToOne
association on the child side which themappedBy
references.The unidirectional
@OneToMany
association does not perform very well, so you should avoid it.You are better off using the bidirectional
@OneToMany
which is more efficient.The annotation mappedBy ideally should always be used in the Parent side (Company class) of the bi directional relationship, in this case it should be in Company class pointing to the member variable 'company' of the Child class (Branch class)
The annotation @JoinColumn is used to specify a mapped column for joining an entity association, this annotation can be used in any class (Parent or Child) but it should ideally be used only in one side (either in parent class or in Child class not in both) here in this case i used it in the Child side (Branch class) of the bi directional relationship indicating the foreign key in the Branch class.
below is the working example :
parent class , Company
child class, Branch
@JoinColumn
could be used on both sides of the relationship. The question was about using@JoinColumn
on the@OneToMany
side (rare case). And the point here is in physical information duplication (column name) along with not optimized SQL query that will produce some additional UPDATE statements.According to documentation:
Since many to one are (almost) always the owner side of a bidirectional relationship in the JPA spec, the one to many association is annotated by @OneToMany(mappedBy=...)
Troop has a bidirectional one to many relationship with Soldier through the troop property. You don't have to (must not) define any physical mapping in the mappedBy side.
To map a bidirectional one to many, with the one-to-many side as the owning side, you have to remove the mappedBy element and set the many to one @JoinColumn as insertable and updatable to false. This solution is not optimized and will produce some additional UPDATE statements.
The annotation
@JoinColumn
indicates that this entity is the owner of the relationship (that is: the corresponding table has a column with a foreign key to the referenced table), whereas the attributemappedBy
indicates that the entity in this side is the inverse of the relationship, and the owner resides in the "other" entity. This also means that you can access the other table from the class which you've annotated with "mappedBy" (fully bidirectional relationship).In particular, for the code in the question the correct annotations would look like this: