&quort“ org.hibernate.persistentObjectException:持久的独立实体”错误,我可以解决我的头
我最近开始与JPA和Hibernate合作进行学校项目,它基本上是在数据库中存储大陆,国家和城市。
在尝试在实体上使用持久方法时,我会得到标题中提到的错误。
我访问过的任何线程都会解决更复杂的问题,并且并没有真正帮助我的情况。我希望通过在此处发布一些指导。
这是数据库创建脚本。这是一个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)
);
基本上,国家表将大陆表的ID用作外键,而城市表再次将餐桌的ID用作外键。
我得到的错误是由于我试图坚持一个新的大陆实体而引起的。这是执行的代码:
public static void main(String[] args) {
EntityManagerFactory ef = Persistence.createEntityManagerFactory("default");
EntityManager em = ef.createEntityManager();
EntityTransaction transaction = em.getTransaction();
try{
transaction.begin();
ContinentsEntity continentEntity = new ContinentsEntity();
continentEntity.setId(1);
continentEntity.setName("Europe");
em.persist(continentEntity);
transaction.commit();
} finally {
if(transaction.isActive()){
transaction.rollback();
}
em.close();
ef.close();
}
}
所有实体类都是由Intellij通过持久工具生成的。
这是课程。
大陆:
package entity;
import javax.persistence.*;
@Entity
@Table(name = "continents", schema = "public", catalog = "postgres")
public class ContinentsEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@Basic
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ContinentsEntity that = (ContinentsEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
国家意义:
package entity;
import javax.persistence.*;
@Entity
@Table(name = "countries", schema = "public", catalog = "postgres")
public class CountriesEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "code")
private String code;
@Basic
@Column(name = "continent_id")
private Integer continentId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Integer getContinentId() {
return continentId;
}
public void setContinentId(Integer continentId) {
this.continentId = continentId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CountriesEntity that = (CountriesEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (code != null ? !code.equals(that.code) : that.code != null) return false;
if (continentId != null ? !continentId.equals(that.continentId) : that.continentId != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (code != null ? code.hashCode() : 0);
result = 31 * result + (continentId != null ? continentId.hashCode() : 0);
return result;
}
}
城市:
package entity;
import javax.persistence.*;
@Entity
@Table(name = "cities", schema = "public", catalog = "postgres")
public class CitiesEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@Basic
@Column(name = "country_id")
private Integer countryId;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "hascapital")
private Boolean hascapital;
@Basic
@Column(name = "latitude")
private Double latitude;
@Basic
@Column(name = "longitude")
private Double longitude;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Integer getCountryId() {
return countryId;
}
public void setCountryId(Integer countryId) {
this.countryId = countryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getHascapital() {
return hascapital;
}
public void setHascapital(Boolean hascapital) {
this.hascapital = hascapital;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CitiesEntity that = (CitiesEntity) o;
if (id != that.id) return false;
if (countryId != null ? !countryId.equals(that.countryId) : that.countryId != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (hascapital != null ? !hascapital.equals(that.hascapital) : that.hascapital != null) return false;
if (latitude != null ? !latitude.equals(that.latitude) : that.latitude != null) return false;
if (longitude != null ? !longitude.equals(that.longitude) : that.longitude != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (countryId != null ? countryId.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (hascapital != null ? hascapital.hashCode() : 0);
result = 31 * result + (latitude != null ? latitude.hashCode() : 0);
result = 31 * result + (longitude != null ? longitude.hashCode() : 0);
return result;
}
}
我的数据库设计或Intellij生成我的实体类的方式可能是某种东西,但我只是无法弄清楚。
编辑:我的数据库设计是故障,也是我试图坚持ID的错误。我修改了所有PK的序列化,并删除了我添加ID的代码行,这可以解决问题。
I've recently started working with JPA and Hibernate for a school project, and it basically stores continents, countries and cities in a database.
When trying to use the persist method on an entity, I get the error mentioned in the title.
Whichever threads I've visited tackle a lot more complex problems, and don't really help my case. I'm hoping to find some guidance by posting here.
This is the database creation script. It's a PostgreSQL database.
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)
);
Basically, the countries table uses the continents table's ID as a foreign key, and the cities table uses the countries table's ID again, as a foreign key.
The error I get is caused by me trying to persist a new continent entity. This is the code that gets executed:
public static void main(String[] args) {
EntityManagerFactory ef = Persistence.createEntityManagerFactory("default");
EntityManager em = ef.createEntityManager();
EntityTransaction transaction = em.getTransaction();
try{
transaction.begin();
ContinentsEntity continentEntity = new ContinentsEntity();
continentEntity.setId(1);
continentEntity.setName("Europe");
em.persist(continentEntity);
transaction.commit();
} finally {
if(transaction.isActive()){
transaction.rollback();
}
em.close();
ef.close();
}
}
All the entity classes are generated by Intellij, through the persistence tool.
Here are the classes.
ContinentsEntity:
package entity;
import javax.persistence.*;
@Entity
@Table(name = "continents", schema = "public", catalog = "postgres")
public class ContinentsEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@Basic
@Column(name = "name")
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ContinentsEntity that = (ContinentsEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
return result;
}
}
CountriesEntity:
package entity;
import javax.persistence.*;
@Entity
@Table(name = "countries", schema = "public", catalog = "postgres")
public class CountriesEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "code")
private String code;
@Basic
@Column(name = "continent_id")
private Integer continentId;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Integer getContinentId() {
return continentId;
}
public void setContinentId(Integer continentId) {
this.continentId = continentId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CountriesEntity that = (CountriesEntity) o;
if (id != that.id) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (code != null ? !code.equals(that.code) : that.code != null) return false;
if (continentId != null ? !continentId.equals(that.continentId) : that.continentId != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (code != null ? code.hashCode() : 0);
result = 31 * result + (continentId != null ? continentId.hashCode() : 0);
return result;
}
}
CitiesEntity:
package entity;
import javax.persistence.*;
@Entity
@Table(name = "cities", schema = "public", catalog = "postgres")
public class CitiesEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
@Column(name = "id")
private int id;
@Basic
@Column(name = "country_id")
private Integer countryId;
@Basic
@Column(name = "name")
private String name;
@Basic
@Column(name = "hascapital")
private Boolean hascapital;
@Basic
@Column(name = "latitude")
private Double latitude;
@Basic
@Column(name = "longitude")
private Double longitude;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Integer getCountryId() {
return countryId;
}
public void setCountryId(Integer countryId) {
this.countryId = countryId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Boolean getHascapital() {
return hascapital;
}
public void setHascapital(Boolean hascapital) {
this.hascapital = hascapital;
}
public Double getLatitude() {
return latitude;
}
public void setLatitude(Double latitude) {
this.latitude = latitude;
}
public Double getLongitude() {
return longitude;
}
public void setLongitude(Double longitude) {
this.longitude = longitude;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CitiesEntity that = (CitiesEntity) o;
if (id != that.id) return false;
if (countryId != null ? !countryId.equals(that.countryId) : that.countryId != null) return false;
if (name != null ? !name.equals(that.name) : that.name != null) return false;
if (hascapital != null ? !hascapital.equals(that.hascapital) : that.hascapital != null) return false;
if (latitude != null ? !latitude.equals(that.latitude) : that.latitude != null) return false;
if (longitude != null ? !longitude.equals(that.longitude) : that.longitude != null) return false;
return true;
}
@Override
public int hashCode() {
int result = id;
result = 31 * result + (countryId != null ? countryId.hashCode() : 0);
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (hascapital != null ? hascapital.hashCode() : 0);
result = 31 * result + (latitude != null ? latitude.hashCode() : 0);
result = 31 * result + (longitude != null ? longitude.hashCode() : 0);
return result;
}
}
It might be something with my DB design, or the way Intellij generated my entity classes, but I just can't figure it out.
EDIT: My DB design was the fault, as well as me trying to persist the id. I modified all pk's to be serial, as well as removing the line of code where I added the id, and that did the trick.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题是,您正在尝试坚持一个设置ID的实体,Hibernate认为它是现有的实体,但在持久性上下文中找不到它。尝试在没有ID的情况下坚持实体,应自动生成。
The problem is, that you're trying to persist an entity with id being set and Hibernate thinks it's an existing entity, but can't find it in it's Persistence Context. Try to persist the entity without the ID, it should be generated automatically.
我只是有同样的事情,并从一个实体的ID中删除了@generatedValue注释,该实体在OneTomany关系中与另一个实体一起解决了它。
I just had the same thing and removing the @GeneratedValue annotation from the id of an entity that is joined by another entity in a OneToMany relationship solved it.
我面临这个问题。然后删除
(cascade = cascadeType.all)
我解决了我的问题。I faced this problem. Then after removing
(cascade = CascadeType.ALL)
my problem solved.