In your scenario, the mappedBy attribute should not have any effect.
I was confused by the name of the primary key (it differs from "id"), but using a non-standard name as an identifier (somehow, as expected) does not cause the problem that you are encountering.
I experimented with the mappedBy parameter in the context of the OneToMany and OneToOne associations and would like to share my findings. I have a similar scenario for testing purposes and to illustrate the impact.
Person.class:
@Entity public class Person { @Id @GeneratedValue private Long id; @Column(name = "name") private String name; @OneToOne private Address address; }
Address.class (for OneToOne tests):
@Entity public class Address { @Id @GeneratedValue private Long id; @Column(name = "address") private String address; @OneToOne(mappedBy="address") private Person person; @OneToMany(cascade = CascadeType.ALL) private Set<Phone> phone = new HashSet<Phone>(); }
Phone.class (for OneToMany tests):
@Entity public class Phone { @Id @GeneratedValue private Long id; @ManyToOne(optional = false) private Person person; @Column(name = "number") private String number; }
By moving the "mappedBy" parameter for the OneToOne association, in any case, the sql statements executed by hibernation remain the same:
Hibernate: insert into Address (id, address) values (default, ?) Hibernate: insert into Person (id, address_id, name) values (default, ?, ?)
For OneToMany associations , I found that if you do not specify "mappedBy", hibernate automatically uses the join table, while with the parameter "mappedBy" the association is displayed as a separate column in the entity table.
If I use ...
@OneToMany(cascade = CascadeType.ALL) private Set<Phone> phone = new HashSet<Phone>();
... Transferring a person with one phone entry leads to the following operations that must be performed:
Hibernate: insert into Phone (id, number, person_id) values (default, ?, ?) Hibernate: insert into Person_Phone (Person_id, phone_id) values (?, ?)
While using ...
@OneToMany(mappedBy="id", cascade = CascadeType.ALL) private Set<Phone> phone = new HashSet<Phone>();
leads to a slightly different relational model, which is closer to what might be expected in this case:
Hibernate: insert into Phone (id, number, person_id) values (default, ?, ?)