一尘不染

JPA中的参照完整性约束违反错误

spring-boot

我正在尝试解析Web请求并将其保存到数据库。我有3个模型,第一个节点是virtualDocument。这是uniq表(根据请求url)。VirtualRequest表具有所有erquest主体,而HttpHeaderList表根据其virtualRequest
bean ID具有所有thhp标头。

当我尝试保存第一个日志时,出现了这样的错误;

org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FK1TW2G47F7A47580KQVMDJWGBQ: PUBLIC.T_VIRTUAL_REQUEST FOREIGN KEY(REQUEST_ID) REFERENCES PUBLIC.T_VIRTUAL_DOCUMENT(DOCUMENT_ID) (65)"; SQL statement:
insert into t_virtual_request (request_id, media_type, method_type, request_url) values (null, ?, ?, ?) [23506-192]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.192.jar:1.4.192]

这是VirtualDocument bean

@Entity
@Table(name = "t_virtual_document")
public class VirtualDocument {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "document_id")
    private long documentId;

    @Column(name = "real_url", unique = true)
    private String realURL; //uniq

    @Column(name = "virtual_url", unique = true)
    private String virtualURL; //uniq

    @Column(name = "simulation_mode", columnDefinition = "varchar(10) default 'STOP'")
    private String simulationMode;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "request_id")
    private List<VirtualRequest> requestList;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "response_id")
    private List<VirtualResponse> responseList;

   //getter setter without any annotation
}

这是VirtualRequest bean;

@Entity
@Table(name = "t_virtual_request")
public class VirtualRequest {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "request_id")
    private long requestId;

    @Column(name = "request_url")
    private String requestURL;

    @Column(name = "method_type")
    private String methodType;

    @Column(name = "media_type")
    private String mediaType;

    @OneToMany(cascade = CascadeType.ALL)
    @JoinColumn(name = "header_id")
    private List<HttpHeaderList> requestHeaders;

   //getter setter without any annotation
}

这是HeaderList bean;

@Entity
@Table(name = "t_http_headers")
public class HttpHeaderList {

    @Id
    @Column(name = "header_id")
    private long headerId;

    @Column(name = "header_key")
    private String headerKey;

    @Column(name = "header_value")
    private String headerValue;
}

阅读 537

收藏
2020-05-30

共1个答案

一尘不染

我认为这是您想要的:

@Entity
@Table(name = "t_virtual_document")
public class VirtualDocument {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "document_id")
    private long documentId;

    @Column(name = "real_url", unique = true)
    private String realURL; //uniq

    @Column(name = "virtual_url", unique = true)
    private String virtualURL; //uniq

    @Column(name = "simulation_mode", columnDefinition = "varchar(10) default 'STOP'")
    private String simulationMode;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "virtualDocument")
    private List<VirtualRequest> requestList;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "virtualDocument")
    // Note the mappedBy parameter. This points to the property in the entity that owns the relationship (in this case the VirtualResponse).
    private List<VirtualResponse> responseList;

   //getter setter without any annotation
}

@Entity
@Table(name = "t_virtual_request")
public class VirtualRequest {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "request_id")
    private long requestId;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "document_id")
    private VirtualDocument virtualDocument;

    @Column(name = "request_url")
    private String requestURL;

    @Column(name = "method_type")
    private String methodType;

    @Column(name = "media_type")
    private String mediaType;

    @OneToMany(cascade = CascadeType.ALL, mappedBy = "virtualRequest")
    private List<HttpHeaderList> requestHeaders;

   //getter setter without any annotation
}

@Entity
@Table(name = "t_http_headers")
public class HttpHeader { /*Note this is a more appropriate name for the entity since it holds the data of a single header.*/

    @Id
    @Column(name = "header_id")
    private long headerId;

    @Column(name = "header_key")
    private String headerKey;

    @Column(name = "header_value")
    private String headerValue;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "request_id")
    private VirtualRequest virtualRequest

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "response_id")
    private VirtualResponse virtualResponse;
}

更新了答案,以添加将标头映射到请求实体。

2020-05-30