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

问题描述 投票:0回答:1

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

我正在尝试使用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进行实现的方式如下(为简洁起见,缺少getter / setter):

驱动程序

@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中的持久函数调用(clientGet通过成功的查询获得,并且insert只是查询的类):

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

    inserts.insertEntity(d);

insertEntity函数:

   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的值已经在Rentals表中一样,但是Rental实体取决于驱动程序存在的先存。数据库的SQL似乎没有这样的约束。

有人可以帮助我了解我在做什么错吗?我尝试了所有发现的东西,但对此没有任何启发。

java sql hibernate jpa jpql
1个回答
0
投票

使用租金和驾驶员之间的多对多关系。我在PurchaseOrder和PaymentMethods关系中曾经遇到的相同问题。

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

© www.soinside.com 2019 - 2024. All rights reserved.