gpt4 book ai didi

java - 如何坚持使用 Hibernate

转载 作者:行者123 更新时间:2023-12-02 12:57:48 25 4
gpt4 key购买 nike

我有一个名为“ token 请求者”和“证书”的类。每个 token 请求者都有许多证书,并且证书只有一个 token 请求者。

TokenRequestor.java

@Entity
@Table(name = "TOKEN_REQUESTOR")
public class TokenRequestor implements Serializable {

@Id
@SequenceGenerator(name = "TOKEN_REQUESTOR_ID_GENERATOR", sequenceName = "TOKEN_REQUESTOR_ID")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "TOKEN_REQUESTOR_ID_GENERATOR")
@Column(name = "TOKEN_REQUESTOR_ID")
private Long id;
@OneToMany(mappedBy = "tokenRequestorId")
private List<Certificate> certificates;

证书.java

@Entity
@Table(name = "CERTIFICATES")
public class Certificate {

@Id
@SequenceGenerator(name = "CERTIFICATES_ID_GENERATOR", sequenceName = "SQ_CERTIFICATES_ID")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "CERTIFICATES_ID_GENERATOR")
@Column(name = "CERTIFICATES_ID")
private Long id;
@ManyToOne(fetch = FetchType.EAGER,cascade = CascadeType.ALL)
@JoinColumn(name = "TOKEN_REQUESTOR_ID")
private TokenRequestor tokenRequestorId; //Foreign key*/

当我插入 token 请求者时,必须创建它们的证书。它们已创建,但证书表中的 TOKEN_REQUESTOR_ID 为空。为什么会出现这种情况?

POST token 请求者

{
"domain": "GTW",
...,
"certificates": [
{
"usage": "ENC",
"alias": "encryption_cert",
"content": "-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----"
},
{
"usage": "DEC",
"alias": "decryption_cert",
"content": "-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----"
},
{
"usage": "SSL",
"alias": "communication_cert",
"content": "-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----"
},
{
"usage": "CA",
"alias": "TSP ROOT CA",
"content": "-----BEGIN CERTIFICATE----- ... -----END CERTIFICATE-----"
}
]
}

最佳答案

您的问题是您将关系定义为双向的,这基本上意味着关系的每一方都应该引用另一方。

假设我们有 TokenRequestor 对象,该对象有两个证书,只有当关系是单向的(您可以将您的关系转向解决此问题)时,这才足够,对于双向关系,您需要每个都有一个持有对其 TokenReguestor 的引用的证书。

关于java - 如何坚持使用 Hibernate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44368083/

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