I just want to answer the third question because it is not fully explained.
Here is your question:
Why only the husband’s member @OnetoOne (mappedBy = "Wife"), but the wife’s member does not have @OnetoOne (mappedBy = "husband")
Firstly, something is wrong here: it should be: @OneToOne(mappedBy="wife") , not Wife . Note that mappedBy must be followed by an attribute Wife Husband class Wife , not a Wife class , is not a Husband class name although the Husband class is the owner. The Hibernate document says:
mappedBy refers to the name of the association property on the owner side.
Secondly, you need to keep in mind that when comparing a database, there are two types of relationships: unidirectional and bidirectional . We use unidirectional relationships when we want one part to process / support this relationship, but on the other hand we are not interested in receiving data from this side. In your example, if we just need a one-way relationship from the Husband class to Wife , we only want to know, having an object of the Husband type, who is his wife (using the getWife() method), but we are not interested in knowing one lady husband.
In this case, we can do the following: (note that in the Husband class we have an attribute of the Wife type, but in the Wife class we do not have an attribute of the Husband type)
Husband.java:
@Entity public class Husband implements Serializable { ... @OneToOne
Wife.java:
@Entity public class Wife implements Serializable { @Id private int id; private String name;
But, if we want to have a bi-directional connection, we must match it both on the side and on the side belonging to it, mappedBy necessary before the attribute belongs.
We remain the Husband side intact, and we change Wife.java :
Wife.java:
@Entity public class Wife implements Serializable { @Id private int id; private String name; @OneToOne(fetch=FetchType.LAZY, mappedBy="wife") private Husband husband; }
Check this page: https://en.wikibooks.org/wiki/Java_Persistence/OneToOne , this explains all this in an understandable way.
Thirdly, just a small note: the owner side is usually a table class with the FK of another table. Or, we just remember: the owner owns all this, so he also has an FK in his table.