gpt4 book ai didi

java - JPQL插入新实体违反外键约束

转载 作者:太空宇宙 更新时间:2023-11-04 09:14:32 25 4
gpt4 key购买 nike

更新:更改了代码以与数据库模型保持一致,并更改了模型本身的详细信息。还添加了导致错误的部分的代码。

我正在尝试使用 PostgreSQL 在 Hibernate 中实现一个小型数据库咨询系统,并且遇到了一对特定表的问题。正如您所看到的,这是一个汽车租赁服务系统,表格存储司机和租金。司机应该能够多次租赁(但反之则不然)。

问题表

CREATE TABLE Driver(
cod_driver SERIAL PRIMARY KEY,
cod_client INTEGER,
num_license BIGINT UNIQUE,
expiration_license DATE,
ident_driver BIGINT,

FOREIGN KEY (cod_client)
REFERENCES Client(cod_client)
ON UPDATE CASCADE
ON DELETE CASCADE
);


CREATE TABLE Rental(
cod_rental SERIAL PRIMARY KEY,
cod_plate VARCHAR(10),
cod_dest VARCHAR(10),
cod_driver INTEGER,
date_delivery DATE,

FOREIGN KEY (cod_plate)
REFERENCES Vehicle(cod_plate)
ON UPDATE CASCADE
ON DELETE NO ACTION,

FOREIGN KEY (cod_dest)
REFERENCES Location(cod_location)
ON UPDATE CASCADE
ON DELETE NO ACTION,

FOREIGN KEY (cod_driver)
REFERENCES Driver(cod_driver)
ON UPDATE CASCADE
ON DELETE CASCADE
);

我使用 Hibernate 进行了如下实现(为简洁起见,缺少 getters/setters):

驱动程序

@Entity
public class Driver {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer cod_driver;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cod_client")
private Client client;

private Long num_license;
private Long ident_driver;
private LocalDate expiration_license;
}

出租

@Entity
public class Rental {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer cod_rental;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cod_plate")
private Vehicle vehicle;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cod_dest")
private Location location_dest;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "cod_driver")
private Driver driver;

private LocalDate date_delivery;

PSQLException 的上下文

Main.java中的Persist函数调用(clientGet是通过查询成功获得的,inserts只是一个用于查询的类):

            Driver d = new Driver(clientGet, 3294324792L, 321312931L, LocalDate.of(2030, 10, 01));

inserts.insertEntity(d);

插入实体函数:

   public void insertEntity(Object o) // Basic insertion of any persistent object
{
em.getTransaction().begin();
em.persist(o);
em.getTransaction().commit();
}

错误

我得到的错误是这样的:

Caused by: org.postgresql.util.PSQLException: 
ERROR: insert or update on table "driver" violates foreign key
constraint "fkdfq0qhvpkw1dqguk6dv1dsj0t"
Detail: Key (cod_driver)=(3) is not present in table "rental".
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2497)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2233)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310)
at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:446)
at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:370)
at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:149)
at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:124)
at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:175)

我的考虑

根据我的理解,鉴于我不在司机表中存储租金,这种关系不需要是双向的(尽管我确实尝试使用mappedBy)。

我只是不明白这可能违反什么约束。就好像它期望 cod_driver 的值已经在租金表中一样,但租赁实体取决于驾驶员的预先存在。数据库的 SQL 似乎没有任何这样的约束。

有人可以帮助我理解我做错了什么吗?我尝试了所有发现的东西,但没有任何线索说明这一点。

最佳答案

在租赁和司机之间使用多对多关系。我在 PurchasingOrder 和 PaymentMethods 关系中也遇到过同样的问题。

@ManyToMany(级联 = { CascadeType.PERSIST, CascadeType.MERGE }) @JoinTable(name = "rental_driver", joinColumns = @JoinColumn(name = "rental_id"), inverseJoinColumns = @JoinColumn(name = "driver_id")) 私有(private)列表驱动程序 = new List();

关于java - JPQL插入新实体违反外键约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59223766/

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