外键为空Springboot jpa

8aqjt8rx  于 2023-02-12  发布在  Spring
关注(0)|答案(1)|浏览(149)

顾客

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor

@Entity
@Table(name = "customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "customer_id")
    private Long customerId;

    @NonNull
    @Column(name = "name")
    private String name;

    @NonNull
    @Column(name = "address")
    private String address;

    @NonNull
    @Column(name = "house_no")
    private String houseNo;

    
    @Column(name = "active")
    private boolean active = true;

    @NonNull
    @Column(name = "customer_type")
    private String customerType;

    @NonNull
    @Column(name = "pack")
    private String pack;

    @JsonIgnore
    @OneToOne(mappedBy = "customer",cascade = CascadeType.ALL)
    private Stb stb;

    @JsonIgnore
     @OneToOne(mappedBy = "customer",cascade = CascadeType.ALL )
    private Payment payment;

    @JsonIgnore
    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL)
    private List<History> history;

}

历史

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor

@Entity
@Table(name = "history")
public class History {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "history_id")
    private Long historyId;

    @Column(name = "amount_paid")
    private Long AmountPaid;

    

    @LastModifiedDate
    @Column(name = "payment_date")
    private String paymentDate;

   
    @Column(name = "due")
    private Long due;

    @JsonIgnore
    @ManyToOne(optional = false,fetch=FetchType.LAZY)
    @JoinColumn(name = "customer_f_id",referencedColumnName = "customer_id")
    private Customer customer;

付款

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "payment")
public class Payment {
    public Long normalPrice =220L;
    public Long sportsPrice = 250L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "payment_id")
    private Long paymentId;

    @Nonnull
    @Column(name = "amount_paid")
    private Long paid;

    @Nullable
    @LastModifiedDate
    @Column(name = "payment_date")
    private String paymentDate;

    @Nullable
    @Column(name = "due")
    private Long due;

    @JsonIgnore
    @OneToOne(fetch=FetchType.EAGER,optional=false)
    @JoinColumn(name = "customer_f_id",referencedColumnName = "customer_id")
    private Customer customer;

机顶盒

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "stbox")
public class Stb {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "stb_id")
    private Long StbId;

    @NonNull
    @Column(name = "stbox_number")
    private String StboxNumber;

    @NonNull
    @Column(name = "stbox_id")
    private String StboxId;

    @NonNull
    @Column(name = "stbox_cust_number")
    private String StboxCustNumber;
    
    @NonNull
    @Column(name = "stbox_type")
    private String StboxType;
    
    @JsonIgnore
    @OneToOne(optional = false,fetch=FetchType.LAZY)
    @JoinColumn(name = "customer_f_id",referencedColumnName = "customer_id")
    private Customer customer;

我是springboot的新手,我只是用很多方法分配了外键我看了很多教程和博客,尝试了一下,但都失败了,外键总是设置为null,任何人帮助,提前感谢:)。
我试图创建一个外部关键字在机顶盒,付款,历史,但我引用它正确,但它的ssets为空

6jjcrrmo

6jjcrrmo1#

您是否设置了连接的两侧?

public class Customer {
  public void setStb(Stb stb) {
    this.stb = stb;
    stb.customer = this;
  }
}

或者在外面

public void setCustomerStb(Customer customer, Stb stb) {
  customer.setStb(stb);
  stb.setCustomer(customer);
}

这同样适用于其他联接。

相关问题