简体   繁体   English

Hibernate如何坚持

[英]How persist with Hibernate

I have a class named "Token Requestor" and "Certificates". 我有一个名为“令牌请求者”和“证书”的类。 Each Token Requestor have many Certificates and Certificates have only one Token Requestor. 每个令牌请求者有许多证书,并且证书只有一个令牌请求者。

TokenRequestor.java 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*/

When I insert a Token Requestor, they certificate must be create. 当我插入令牌请求者时,必须创建它们的证书。 They are created but with TOKEN_REQUESTOR_ID null in Certificate table. 它们已创建,但证书表中的TOKEN_REQUESTOR_ID为空。 Why this happens? 为什么会这样?

POST Token Requestor 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-----"
    }
  ]
}

your problem is that you define the relationship to be bidirectional, which basically means each side of the relation should have reference to the other side. 您的问题是将关系定义为双向,这基本上意味着关系的每一侧都应引用另一侧。

lets say we have TokenRequestor object , this object has two Certificates, this would be enough only if the relation is unidirectional ( which you can turn your relation to to solve this issue btw), for a bidirectional relation you need to haveeach one of the Certificate holding a reference to their TokenReguestor. 假设我们有TokenRequestor对象,该对象有两个证书,仅当关系是单向的(您可以将您的关系转为解决此问题)时,这才足够;对于双向关系,您需要每个证书中的一个持有对其TokenReguestor的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM