One To One Association strategy

Hi All, In one to one mapping is it ok to add column foreign key in parent like

@Entity(name = "Post")
@Table(name = "post")
public class Post {

	@Id
	@GeneratedValue
	private Long id;

	@OneToOne(cascade = CascadeType.ALL)
	@JoinColumn(name = "postDetails_id")
	private PostDetails postDetails;
}
@Entity(name = "PostDetails")
@Table(name = "post_details")
public class PostDetails {

	@Id
	@GeneratedValue
	private Long id;

	@OneToOne(mappedBy = "postDetails")
	private Post post;
}

Rather then
Setting foreign key in post_details table (i.e post_id)

@Entity(name = "PostDetails")
@Table(name = "post_details")
public class PostDetails {
 
    @Id
    @GeneratedValue
    private Long id;

 
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "post_id")
    private Post post;
 
  
 
    
}

That’s not really a one-to-one association, but a many-to-one with a unique constraint.

Check out this article for more details about how a one-to-one table relationship works.

Also, check out this article to learn the best way you could map that with JPA and Hibernate.

Now, back to your question:

is it ok to add column foreign key in parent like

No, it’s not. It’s a bad idea as it creates a circular dependency.

Thank you Vlad
So what i understood from that mapping is it’s good to add FK in child.

Thank You
Salman