0

I investigating ways of create one to one relation between two entities in hibernate.

All examples which I have read use cross referernces:

https://stackoverflow.com/a/21762450/2674303

and

http://www.mkyong.com/hibernate/hibernate-one-to-one-relationship-example-annotation/

At this example:

public class User {
    @OneToOne(mappedBy = "user")
    private Status status;
    // ...
}

public class Status {
    @OneToOne
    @JoinColumn(name = "frn_user_id")
    private User user;
    // ...
}

User has reference to Status and Status has reference to User

In my opinion Status shouldn't know anything about User. I want that only User have reference to Status.

Is it possible or I don't understand something?

Community
  • 1
  • 1
gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

1 Answers1

2

You can try this for User (remove reference to User in Status)

public class User {
    @OneToOne
    @JoinColumn(name = "frn_user_id", insertable = false, updateable = false)
    private Status status;
    // ...
}

insertable = false, updateable = false should instruct Hibernate to look for frn_user_id in target table (Status).

Predrag Maric
  • 23,938
  • 5
  • 52
  • 68