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,090 points
9,029 views

1 answer 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,890 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.

Related Questions In Java

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,580 points
426 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,190 points
2,029 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,150 points
349 views
0 votes
2 answers

What is the difference between = and equals()?

The equals() method compares the "value" inside String instances ...READ MORE

answered Aug 13, 2018 in Java by Daisy
• 8,090 points
162 views
+1 vote
10 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
18,230 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,890 points
204 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,630 points
15,041 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,090 points
881 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,890 points
223 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,890 points
716 views