title: Data was not saved: object references an unsaved transient instance - save the transient instance before flushing
body: <p>I have a database with two tables User and Country. I want to relationship where many user can belong to one county. I implement this using hibernate using the following model classes: </p>
  
  <pre><code>@Entity (name = "user")
  public class User {
  
     @Id @GeneratedValue (strategy = GenerationType.IDENTITY)
      private int userId;
      private String username;
      private String password;
  
     @ManyToOne ()
     @JoinColumn (name = "countryId")
     private Country country;
  
      //Getter &amp; Setter
   }
  
  
  @Entity (name = "country")
  public class Country {
  
      @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
      private int countryId;
      private String countryName;
     //Getter &amp; Setter
  }
  </code></pre>
  
  <p>When I try to save the user object I get the following exception:</p>
  
  <pre><code>  org.hibernate.HibernateException: Data was not saved: object references an unsaved transient instance - save the transient instance before flushing: com.kyrogaming.models.Country
  </code></pre>
  
  <p>How can I fix the problem?</p>