简体   繁体   中英

bidirectional one-to-one relationship (mappedBy)

You create a bidirectional one-to-one relationship using fields on both classes, with an annotation on the child class's field to declare that the fields represent a bidirectional relationship. The field of the child class must have a @Persistent annotation with the argument mappedBy = "...", where the value is the name of the field on the parent class. If the field on one object is populated, then the corresponding reference field on the other object is populated automatically.

What can a bidirectional one-to-one relationship do?

Can someone give me a example?

And why I always got this error.

Class "com.example.datastore.Employee" has field "contactInfo" with "mapped-by" specified as "contactInfo". This field doesnt exist in the target of the relation ("com.example.datastore.ContactInfo")!!

Thanks in advance!

I try to answer from what I learnt from Hibernate/JPA (which I think is similar)

Seems that your ContactInfo do not have relationship to Employee. To use what you described as an example for bidirectional one-to-one relationship, you will see something like (it is probably not syntactically correct, just to give u idea):

public class Employee {
  //... other relationship or fields

  @OneToOne(mappedBy="employee")  // the field in ContactInfo
  private ContactInfo contactInfo;

}

public class ContactInfo {
  @OneToOne
  @JoinColumn("EMP")
  private Employee employee;
}

The 'real' relationship in persistence layer is in fact dominated by ContactInfo.employee. Setting Employee.contactInfo will not cause persistence layer to contain correct data.

I wish this help and applies to JDO too. :P

It sounds like you have a one-to-one of Employee to ContactInfo. An employee has exactly one contact info, and a contact info belongs to exactly one employee. That's a bidirectional one-to-one. Your error is occurring because "mapped-by" needs to specify the name of the property of the other object that refers back to this one. For example, if you have

class Employee {
    private ContactInfo contactInfo;
}

class ContactInfo {
    private Employee employee;
}

then when you map the Employee.contactInfo property, its "mapped-by" would be "employee" because that's the property that it's "mapped by" in the ContactInfo.

First the Error
What the error is saying is that contactInfo is not a field of class com.example.datastore.ContactInfo . They field mapped by must be a field\property of the class you are mapping to.
Second The concept of Bi-Direction Mapping
It's just that two tables in one-to-one or one-to-many relationship where both entities will have knowledge of the other s. You do not always need it and that depends on situation and context but generally is more common to have bi-directional one to many than one to one. You question is about one-to-one so to give you an example of when both entities need to know about each other from Hibernate docs A bidirectional one-to-one association on a join table is possible, but extremely unusual. But if you really wanted to be able to get either entiy from which ever table (which is very rare) then you create bi-directional one to one

Person and Address just to quote an example

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