gpt4 book ai didi

java - 数据未保存 : object references an unsaved transient instance - save the transient instance before flushing

转载 作者:太空狗 更新时间:2023-10-29 22:42:33 25 4
gpt4 key购买 nike

<分区>

我有一个包含两个表 User 和 Country 的数据库。我想要许多用户可以属于一个县的关系。我使用以下模型类使用 hibernate 实现了这一点:

@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

我该如何解决这个问题?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com