0

If we have two classes X and Y that have OneToOne relationship between them

class A {
  @OneToOne(mappedBy = a)
  private B b;
}
class B {
  @OneToOne
  private A a;
}

I would like to know if it makes any difference if we method the mappedBy from Class A to Class B..

Hanchen Jiang
  • 2,514
  • 2
  • 9
  • 20

1 Answers1

1

When you both entities (A and B) you can choose one that will have a foreign key.

In your example, the foreign key will be in B, referencing A.

But in A, you want referencing B. So, you say to the JPA: I have this class A with a relationship with B, but it was created in B. So mappedBy is just a reverse reference to get B in this relationship.

@OneToOne(mappedBy = "a")
leandro.dev
  • 345
  • 2
  • 8