What s the difference between JoinColumn and mappedBy when using a JPA OneToMany association

0 votes

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;
    ...
}
Oct 18, 2018 in Java by Daisy
• 8,140 points
21,198 views

2 answers to this question.

0 votes

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 attribute mappedBy 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:

@Entity
public class Company {
    @OneToMany(fetch = FetchType.LAZY, mappedBy = "company")
    private List<Branch> branches;
}

@Entity
public class Branch {
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "companyId")
    private Company company;
}
answered Oct 18, 2018 by Sushmita
• 6,920 points

Hello All,

I could not understand the part "This also means that you can access the other table from the class which you've annotated with "mappedBy" (fully bidirectional relationship)". Till now, I do understand that the relationship made by using the mapped by and join column is unidirectional <in one to one association>, "mappedBy" only signifies the owning entity.

How you are saying the relationship is bidirectional and not unidirectional? could you please explain?

Hi, @There,

Note that: The main difference is that the bidirectional relationship provides navigational access in both directions so that you can access the other side without explicit queries. Also it allows you to apply cascading options to both directions.

0 votes
JPA mapping annotation can be classified as object model annotation and relational model annotation.

Object model annotation is used to specify object data model while relational model annotation to relational data model. Separating these two type annotation into different file to decouple object model and relational data model is the beast practice

@OneToMany is object model annotation and declares a one-to-many unidirectional relationship between entity object. The declared relationship detail can modified with attributes of the annotation @OneToMany.

@JoinColum is relational model annotation. It assumes that the object relationship is mapped as join column rather than table , and declares that  in table corresponding to the owner side entity(entity at many-side in one-to-many object relationship) there is a join column references table corresponding to inverse side(entity at one-side in one-to-many object relationship)
answered Oct 28, 2020 by bjjj
• 140 points

Related Questions In Java

0 votes
1 answer

What's the difference between using "let" and "var"?

The main difference is scoping rules wherein ...READ MORE

answered Feb 17, 2022 in Java by Aditya
• 7,680 points
615 views
0 votes
1 answer

What's the difference between SoftReference and WeakReference in Java?

Weak references A weak reference, simply put, is a ...READ MORE

answered Jun 12, 2018 in Java by Rishabh
• 3,620 points
1,915 views
0 votes
2 answers

What is the difference between implements and extends?

Extends : This is used to get attributes ...READ MORE

answered Aug 3, 2018 in Java by samarth295
• 2,220 points
16,018 views
0 votes
1 answer

What is the difference between jdk and jre?

JRE: It stands for Java Runtime Environment. ...READ MORE

answered Apr 20, 2018 in Java by Akrati
• 3,190 points
1,874 views
+1 vote
12 answers

Hibernate hbm2ddl.auto possible values and their uses

hibernate.hbm2ddl.auto (e.g. none (default value), create-only, drop, create, create-drop, validate, and update) Setting to perform SchemaManagementTool actions automatically as ...READ MORE

answered Dec 7, 2018 in Java by Shuvodip
76,621 views
0 votes
1 answer

Hibernate show real SQL

If you want to see the SQL ...READ MORE

answered Jul 5, 2018 in Java by Sushmita
• 6,920 points
995 views
0 votes
1 answer

object references an unsaved transient instance - save the transient instance before flushing

You should add cascade="all" (if using xml) ...READ MORE

answered Sep 26, 2018 in Java by Parth
• 4,640 points
34,693 views
0 votes
1 answer

Difference between FetchType LAZY and EAGER in Java Persistence API?

Sometimes you have two entities and there's ...READ MORE

answered Dec 5, 2018 in Java by Daisy
• 8,140 points
2,503 views
0 votes
1 answer

Is there any difference between ConcurrentHashMap and Collections.synchronizedMap?

ConcurrentHashMap: It allows concurrent modification of the ...READ MORE

answered Dec 26, 2018 in Java by Sushmita
• 6,920 points
1,116 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,920 points
1,858 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP