JPA Flashcards

1
Q

Difference Between @JoinColumn and mappedBy
-In a One-to-Many/Many-to-One relationship, the owning side is usually defined on the many side of the relationship. It’s usually the side that owns the foreign key.

A

JPA Relationships can be either unidirectional or bidirectional. This simply means we can model them as an attribute on exactly one of the associated entities or both.

Defining the direction of the relationship between entities has no impact on the database mapping. It only defines the directions in which we use that relationship in our domain model.

For a bidirectional relationship, we usually define

the owning side
inverse or the referencing side
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.
https://www.baeldung.com/jpa-joincolumn-vs-mappedby

***** IS is usually OneToMany and ManyToOne?
EG: an employee can have many email addresses. However, a given email address can belong exactly to a single employee.

public class Employee {
@OneToMany(fetch = FetchType.LAZY, mappedBy = “employee”)
private List<Email> emails;
}</Email>

public class Email {
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = “employee_id”)
private Employee employee;
}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly