我最近開始為一個學校專案使用 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 的代碼行,這就成功了。
uj5u.com熱心網友回復:
問題是,您試圖保留一個設定了 id 的物體,而 Hibernate 認為它是一個現有物體,但在它的持久性背景關系中找不到它。嘗試持久化沒有 ID 的物體,它應該會自動生成。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/477127.html
標籤:爪哇 PostgreSQL 休眠 jpa 实体管理器
