简体   繁体   中英

placement of mappedBy in a OneToOne relationship in Java Hibernate/JPA

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..

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM