我最近开始使用 JPA 和 Hibernate 进行一个学校项目,它基本上将各大洲、国家和城市存储在数据库中。
当尝试在实体上使用 persist 方法时,我收到标题中提到的错误。
无论我访问过哪些线程都解决了很多更复杂的问题,并且对我的情况没有真正帮助。我希望通过在这里发帖找到一些指导。
这是数据库创建脚本。这是一个 PostgreSQL 数据库。
create table continents(
id integer primary key,
name varchar
);
create table countries(
id integer primary key,
name varchar,
code varchar,
continent_ID integer,
constraint fk_continents
foreign key (continent_ID)
references continents(id)
);
create table cities(
id integer primary key,
country_ID integer,
name varchar,
hasCapital boolean,
latitude float,
longitude float,
constraint fk_countries
foreign key (country_ID)
references countries(id)
);
Run Code Online (Sandbox Code Playgroud)
基本上,国家/地区表使用大陆表的 ID 作为外键,而城市表再次使用国家/地区表的 ID 作为外键。
我得到的错误是由于我试图保留一个新的大陆实体而引起的。这是执行的代码:
public static void main(String[] args) { …
Run Code Online (Sandbox Code Playgroud)