3

I've got fk in child, but i need @JoinColumn in parent. By default, in OneToOne, joincolumn creates fk in parent table (where it is specified), i have existing tables with fk in child. (project new demands) Before it was mapped using mappedBy clause.

How can I use @JoinColumn for @OneToOne linking in the parent entity?

I want to make unidirectional relationship.

Why cant I use this:

@JoinColumn(name = "parent_id", referencedColumn = "childs_fk")

ty!

Update:

In this example FK column "WEBSITE_ID" would be in Player table. My FK is in Website table.

In a bi-directional OneToOne relationship, a single foreign key is used in the owning side of the relationship. On the other hand, the target entity must use the mappedBy attribute.

I've got existing tables and my fk is not on the owner side, + I know for a fact that FK could be on both sides. This answer is not what I need.

1 Answers1

0

If you have a table Parent and one table Child and the foreign key is on the Child table with name PARENT_ID and pointing to the ID in table Parent then the annotation must look like:

@JoinColumn(name="PARENT_ID") 
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • There is a lot of FK's in child table beside the one I need. And it's name not in this format: Parent table is `inv_act_acceptance_items_mus`, Child table is `inv_book_museum` Fk is in `inv_book_museum`, and named `act_item_id. ` So the only way is to rename it? – Sergey Dvoreckih Nov 13 '17 at 10:40
  • no no! this is just an example and the name must match your foreign key name – Simon Martinelli Nov 13 '17 at 20:07
  • I meant renaming `act_item_id` to somethig like `inv_act_acceptance_items_mus_id`. Thanks! – Sergey Dvoreckih Nov 14 '17 at 03:50