0
我有一個名爲「令牌請求者」和「證書」的類。每個令牌請求者都有許多證書和證書只有一個令牌請求者。如何堅持休眠
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;
Certificate.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_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資源和/證書資源,可以在兩個資源中創建證書。在這種情況下解決單向關係? –
@DanielaMorais無論你將不得不重試並在證書中注入TokenRequestor,或者通過刪除ManyToOne(及其引用)並將joinColumn放入沒有owningentity(其中OneToMany) –