未保存数据:对象引用未保存的瞬态实例 - 刷新前保存瞬态实例
我有一个数据库,其中包含两个表用户和国家/地区。我想建立关系,其中许多用户可以属于一个县。我使用以下模型类使用休眠来实现这一点:
@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 & Setter
}
@Entity (name = "country")
public class Country {
@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private int countryId;
private String countryName;
//Getter & Setter
}
当我尝试保存用户对象时,我得到以下异常:
org.hibernate.HibernateException: Data was not saved: object references an unsaved transient instance - save the transient instance before flushing: com.kyrogaming.models.Country
如何解决问题?