title: MappedBy in a OneToOne Mapping
body: <p>I have created Three classes- User, UserSubscription and Subscription. I want that users in the UserSubscription table should be referenced by the users existing in the User table. So, how to use mappedBy?
  The classes are as follows:</p>
  
  <p>User:</p>
  
  <pre><code>    package com.model;
  
      import java.util.HashSet;
      import java.util.Set;
  
      import javax.persistence.CascadeType;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.JoinTable;
      import javax.persistence.ManyToMany;
      import javax.persistence.OneToOne;
  
      @Entity
      public class User {
  
  
      private int userId;
      private String password;
      private String contact;  
      private String firstName;
      private String lastName;
      private String gender;
      private String address;
      private int age;
      private String email;
      private String countBooks;
  
  
      private Set&lt;Language&gt; languages = new HashSet&lt;Language&gt;(0);
      private Set&lt;Book&gt; favorites = new HashSet&lt;Book&gt;(0);
  
  
      @Id
      @GeneratedValue
      @Column(name="User_Id")
      public int getUserId() {
          return userId;
      }
  
      public void setUserId(int userId) {
          this.userId = userId;
      }
  
      @Column(name="Password")
      public String getPassword() {
          return password;
      }
  
      public void setPassword(String password) {
          this.password = password;
      }
  
  
      @Column(name="Contact")
      public String getContact() {
          return contact;
      }
  
      public void setContact(String contact) {
          this.contact = contact;
      }
  
  
      @Column(name="First_Name")
      public String getFirstName() {
          return firstName;
      }
  
      public void setFirstName(String firstName) {
          this.firstName = firstName;
      }
  
  
      @Column(name="Last_Name")
      public String getLastName() {
          return lastName;
      }
  
      public void setLastName(String lastName) {
          this.lastName = lastName;
      }
  
  
      @Column(name="Gender")
      public String getGender() {
          return gender;
      }
  
      public void setGender(String gender) {
          this.gender = gender;
      }
  
  
      @Column(name="Address")
      public String getAddress() {
          return address;
      }
  
      public void setAddress(String address) {
          this.address = address;
      }
  
  
      @Column(name="Age")
      public int getAge() {
          return age;
      }
  
      public void setAge(int age) {
          this.age = age;
      }
  
  
      @Column(name="Email")
      public String getEmail() {
          return email;
      }
  
      public void setEmail(String email) {
          this.email = email;
      }
  
  
      @Column(name="Count_Books")
      public String getCountBooks() {
          return countBooks;
      }
  
      public void setCountBooks(String countBooks) {
          this.countBooks = countBooks;
      }
  
      @ManyToMany(cascade=CascadeType.ALL)
      @JoinTable(name="User_Language", joinColumns = { 
              @JoinColumn(name = "User_Id", nullable = false, updatable = false) }, 
              inverseJoinColumns = { @JoinColumn(name = "Language_Id", 
                      nullable = false, updatable = false) })
      public Set&lt;Language&gt; getLanguages() {
          return languages;
      }
  
      public void setLanguages(Set&lt;Language&gt; languages) {
          this.languages = languages;
      }
  
      @ManyToMany
      @JoinTable(name="User_Favorite", joinColumns = { 
              @JoinColumn(name = "User_Id", nullable = false, updatable = false) }, 
              inverseJoinColumns = { @JoinColumn(name = "Book_Id", 
                      nullable = false, updatable = false) })
      public Set&lt;Book&gt; getFavorites() {
          return favorites;
      }
  
      public void setFavorites(Set&lt;Book&gt; favorites) {
          this.favorites = favorites;
      }   
  
  }
  </code></pre>
  
  <p>UserSubscription:</p>
  
  <pre><code>package com.model;
  
  import java.sql.Date;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  import javax.persistence.OneToOne;
  
  @Entity
  public class UserSubscription {
  
      private int userSubscriptionId;
      private Subscription subscription;
      private User user;
      private Date subscriptionStartDate;
      private Date subscriptionEndDate;
      private int status;
  
  
      @Id
      @GeneratedValue
      @Column(name="User_Subscription_Id")
      public int getUserSubscriptionId() {
          return userSubscriptionId;
      }
      public void setUserSubscriptionId(int userSubscriptionId) {
          this.userSubscriptionId = userSubscriptionId;
      }
  
      @Column(name="Subscription_Id")
      public Subscription getSubscription() {
          return subscription;
      }
      public void setSubscription(Subscription subscription) {
          this.subscription = subscription;
      }
      @OneToOne(mappedBy="userId")
      public User getUser() {
          return user;
      }
      public void setUser(User user) {
          this.user = user;
      }
  
      @Column(name="Subscription_Start_Date")
      public Date getSubscriptionStartDate() {
          return subscriptionStartDate;
      }
      public void setSubscriptionStartDate(Date subscriptionStartDate) {
          this.subscriptionStartDate = subscriptionStartDate;
      }
  
      @Column(name="Subscription_End_Date")
      public Date getSubscriptionEndDate() {
          return subscriptionEndDate;
      }
      public void setSubscriptionEndDate(Date subscriptionEndDate) {
          this.subscriptionEndDate = subscriptionEndDate;
      }
  
      @Column(name="Status")
      public int getStatus() {
          return status;
      }
      public void setStatus(int status) {
          this.status = status;
      }
  
  
  
  
  
  }
  </code></pre>
  
  <p>Subscription:</p>
  
  <pre><code>package com.model;
  
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.Id;
  
  @Entity
  public class Subscription {
  
  
      private int subscriptionId;
      private String name;
      private int period;
      private float fees;
      private int noOfBooks;
      private int Status;
  
      @Id
      @GeneratedValue
      public int getSubscriptionId() {
          return subscriptionId;
      }
      public void setSubscriptionId(int subscriptionId) {
          this.subscriptionId = subscriptionId;
      }
      public String getName() {
          return name;
      }
      public void setName(String name) {
          this.name = name;
      }
      public int getPeriod() {
          return period;
      }
      public void setPeriod(int period) {
          this.period = period;
      }
      public float getFees() {
          return fees;
      }
      public void setFees(float fees) {
          this.fees = fees;
      }
      public int getNoOfBooks() {
          return noOfBooks;
      }
      public void setNoOfBooks(int noOfBooks) {
          this.noOfBooks = noOfBooks;
      }
      public int getStatus() {
          return Status;
      }
      public void setStatus(int status) {
          Status = status;
      }
  
  
  
  }
  </code></pre>
  
  <p>Exception while executing:</p>
  
  <pre><code>Exception in thread "main" org.hibernate.AnnotationException: Referenced property not a (One|Many)ToOne: com.model.User.userId in mappedBy of com.model.UserSubscription.user
      at org.hibernate.cfg.OneToOneSecondPass.doSecondPass(OneToOneSecondPass.java:248)
      at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1695)
      at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1424)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
      at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1928)
      at com.impetus.model.Check.main(Check.java:92)
  </code></pre>