我有一个名为“令牌请求者”和“证书”的类。每个令牌请求者有许多证书,并且证书只有一个令牌请求者。
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;
证书文件
@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_REQUESTOR_ID为空。为什么会这样?
POST令牌请求者
{
"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的引用。